How to configure Kendo Dropdownlist to work with Grid popup editor template - kendo-grid

I've been struggling with the Kendo dropdownlist for 2 days now and just can't seem to get it configured correctly.
How do I get the Kendo Dropdownlist to show the current item of the #Model? This is my code:
#Html.TextBoxFor(model => model.ShortDescription, new { #class="wide200;" })
#(Html.Kendo().DropDownList()
.Name("importance")
.HtmlAttributes(new { style = "width: 250px" })
.DataTextField("Name")
.DataValueField("ID")
.DataSource(source => {
source.Read(read =>
{
read.Action("GetImportanceList", "Home");
})
.ServerFiltering(true);
})
.SelectedIndex(0)
)
And in my controller:
public ActionResult GetImportanceList()
{
GenericRepository<Importance> _repository = new GenericRepository<Importance>(_context);
IEnumerable<ImportanceViewModel> list = _repository.Get().ConvertToViewModelList();
return Json(list, JsonRequestBehavior.AllowGet);
}
Problem is with SelectedIndex(0) which is set to the first item. How can I set it to whatever is in the model? It's very simple to do for the textbox (first line in the code): model => model.ShortDescription. But how does this work for the dropdownlist?
I don't just want to set it upon the showing of the editor, but also want the grid to know what the new selection is after I click the Update button.
Note that this is in a custom template for the grid popup editor.

Try this,
You have to pass DropDownListId in model and ListItems.
#(Html.Kendo().DropDownListFor(m=>m.DropDownListId)
.Name("importance")
.HtmlAttributes(new { style = "width: 250px" })
.DataTextField("Name")
.DataValueField("ID")
.DataSource(source => {
source.Read(read =>
{
read.Action("GetImportanceList", "Home");
})
.ServerFiltering(true);
})
.SelectedIndex(0)
)

I asked this question to Telerik. Apparently the Name mustn't be assigned.

Related

Passing a custom component to material ui dialog that opens it

I am trying to pass a custom component to a MUI Dialog in such way that it should open the Dialog itself and render its children.
const CustomDialog = ({children, someCustomComponent}) => {
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
return(
<>
{someCustomComponent} // use this component to call handleOpen/handleClose
<Dialog>
<DialogTitle>
<DialogTItle>
<DialogContent>{children}</DialogContent>
<DialogActions>...</DialogActions>
</Dialog>
</>
);
}
CustomDialog.propTypes = {
someCustomComponent: PropTypes.node.isRequired,
}
And then call it like this
<CustomDialog someCustomComponent={<h1>open</h1>}>
{myDialogContent}
</CustomDialog>
Is this possible? So, essentially, I don't always want a button to open my Dialog. I want to have any component I pass to it to be able to open it.
This is kind of how this is done by using Button
return(
<>
<Button onClick={handleClickOpen} />
<Dialog>
...
but I want to pass any element to it.
Thanks!
A simple way to do it is with React.cloneElement
const CustomDialog = ({ children, someCustomComponent }) => {
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
// clone the component and add the onClick handler
const customComponentClone = React.cloneElement(someCustomComponent, {
onClick: handleClickOpen
});
return (
<>
{customComponentClone}
<Dialog>
<DialogTitle>
<DialogTItle>
<DialogContent>{children}</DialogContent>
<DialogActions>...</DialogActions>
</Dialog>
</>
);
}
This way you can use it like you mentioned
<CustomDialog someCustomComponent={<h1>open</h1>}>
{myDialogContent}
</CustomDialog>
Check here a live version

How do I disable a mvc razor #Html.TextBoxFor based on what is selected in a drop down menu on the same page?

Let's say I have the following.
#Html.DropDownListFor(m => m.MyWebPage, Model.FruitsList, "Select", htmlAttributes: new { #class = "form-select detailsInfo" })
And within this drop down menu I have a selection that says "Custom" and right after that drop down I have a textbox that is autopopulated with the user's already selected option
#Html.TextBoxFor(m => m.MyWebPage, htmlAttributes: new { #class = "form-control", id="CustomFruitTextBox"})
What would I have to call in order to disable the text box if the drop down selection is not currently selected to "Custom"?
Would this require javascript or could I do this in the razor file manually?
You can try to use js to check the selected value of the dropdown:
view:
#Html.DropDownListFor(m => m.MyWebPage, Model.FruitsList, "Select", htmlAttributes: new { #class = "form-select detailsInfo" #onchange="check()" })
js:
<script>
$(function () {
check();
})
function check() {
if ($("#MyWebPage").val() != "Custom") {
$("#CustomFruitTextBox").attr("disabled", "disabled");
} else{
$("#CustomFruitTextBox").removeAttr("disabled");
}
}
</script>

how to use #htmlattributes : for datepicker

hello i'm tryinh to change simple input to date form i tried text
to show a datetime picker with DateTime property
but when i use
#Html.TextBoxFor(model => model.dateFin, new { type = "date" })
but it doesn't work
so i'm using simple input
#Html.EditorFor(model => model.dateDebut, new { htmlAttributes = new {#class = "form-control" } })
#Html.ValidationMessageFor(model => model.dateFin, "", new { #class = "text-danger" })
is there someone who's familiar with this form ?
You can create custom html helper control for datepicker like
public static class HtmlHelper
{
public static IHtmlString DatePicker(this HtmlHelper helper, string id)
{
TagBuilder input = new TagBuilder("input");
input .Attributes.Add("type", "date");
input .Attributes.Add("id", id);
return new MvcHtmlString(input.ToString());
}
}
After creating, you need to refer to this class in your razor page with #using. And you can use like #Html.DatePicker("dt1")

disable textboxfor using a checkboxfor razor mvc and jquery

I am trying to disable/enable an #Html.TextBoxFor in razor using an #Html.CheckBoxFor
I have defined my CheckBoxFor in this manner:
#Html.CheckBoxFor(m => m.ResYearChecked, new { Name = "ResYearChecked", id = "ResYearChecked" })
#Html.TextBoxFor(m => m.SearchResYear, new { Name = "SearchResYear", id = "SearchResYear" })
and I am trying to call the script:
<script>
$('ResYearChecked').change(function () {
$("SearchResYear").prop("disabled", !$(this).is(':checked'));
});
</script>
Do I need to add the onclick attribute to the CheckBoxFor?
I was able to figure out the solution. I enabled the attribute readonly for the #Html.TextBoxFor. I then removed the readonly attribute once the #Html.CheckBoxFor was checked.
Here is the code for my #Html.CheckBoxFor and #Html.TextBoxFor
#Html.CheckBoxFor(m => m.ResYearChecked, new { Name = "ResYearChecked", id = "ResYearChecked" })
#Html.TextBoxFor(m => m.SearchResYear, new { Name = "SearchResYear", id = "SearchResYear", #readonly = "readonly" })
Here is the JavaScript
<script>
$('#ResYearChecked').change(function () {
if ($(this).is(":checked")) {
$("#SearchResYear").removeAttr("readonly");
$("#SearchResYear").focus();
} else {
$("#SearchResYear").attr("readonly", "readonly");
}
});
</script>

How to add class to a kendo grid table header?

Here is my kendo grid,
#(Html.Kendo().Grid<example.Web.Areas.Models.PredictedValues>()
.Name("MyGrid")
.Columns(column =>
{
column.Bound(p => p.Rlno).Title("Roll no").HtmlAttributes(new { #class = "someclass" });
column.Bound(p => p.stdname).Width(60).Title("Std name").HtmlAttributes(new { style = "text-align:right;" });
column.Bound(p => p.school).Width(50).Title("School").HtmlAttributes(new { style = "text-align:right;" });
})
))
I need to apply some stylings through class to the header names. the included #class is not working for the header. Instead, it is bounding to the table rows. How do i do? Any idea?
Did you try:
column.Bound(p => p.Rlno).Title("Roll no").HeaderHtmlAttributes(new { #class = "someclass" });
.HeaderHtmlAttributes adds a class to the header only.
To add a css class to the gridcell - as an example here I am targetting the Status column - you want to style the grid values... you want to do this instead.
columns.Bound(m => m.Status).Title("Status").HtmlAttributes(new { #class = "someclass" });