I am new to Kendo UI and I've searched for what I want to do but without any luck. I have combobox that is filled with fiscal years. I want to select the current one by default which will not always be at the same rank in the combobox. We have a field in the database which is set at 0 when it is the current year. I want to have whichever field that is at 0 as my default value.
Here is the relevant code in my controller. The defaultValue variable is currently not used. I've tried to do something with it without success and I've let it there because it show clearly what I'm trying to accomplish.
public JsonResult ListeAnneeFinanciere()
{
var dimtempsdate = new DimTempsDateViewModel();
try
{
using (var db = new ParcAutoDbContext())
{
var resultat = db.Dim_Temps_Date.Select(a => new { a.ANNEE_FINANCIERE_ID, a.CLASSEMENT })
.Distinct()
.OrderByDescending(a => a.ANNEE_FINANCIERE_ID)
.ToList();
var defaultValue = db.Dim_Temps_Date.Select(a => new { a.ANNEE_FINANCIERE_ID, a.CLASSEMENT })
.Distinct()
.Where(a => a.CLASSEMENT == 0)
.OrderByDescending(a => a.ANNEE_FINANCIERE_ID)
.ToList();
return Json(resultat, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
Log.Instance.Error(ex, message: "Erreur : ListeAnneeFinanciere");
throw new HttpException((int) HttpStatusCode.InternalServerError, message: "500 - Internal Server Error");
}
}
And here is the relevant code in my view. I know I need to set the default value with the value property. I can hardcode it but it's not what I want. I also tried some things in the Databound event but without success. To be honest, I don't really have an idea what to try.
<div class="form-group">
<label class="control-label col-xs-3">Année financière</label>
<div class="col-md-4 col-xs-9">
@{
Html.Kendo().ComboBox()
//.Events(e => e.DataBound("setDefaultYear"))
.Name("AnneeFinanciere")
.Placeholder("Sélectionner une année financière")
.NoDataTemplate("Aucune donnée")
.DataTextField("ANNEE_FINANCIERE_ID")
.DataValueField("ANNEE_FINANCIERE_ID")
.AutoBind(true)
.Suggest(true)
.DataSource(source =>
{
source.Read(read =>
{
read.Action("ListeAnneeFinanciere", "Operation");
})
.ServerFiltering(true);
})
//.Value("2017-2018")
.HtmlAttributes(new { @style = "width:100%" })
.Render();
}
</div>
</div>
I know it must not be that hard but I am a beginner with Kendo UI and not that experimented with MVC. English is not my first language but I hope my question is clear and that someone will be able to help me.
Thank you very much for your time.
From your sample code above since both DataTextField
and DataValueField
is ANNEE_FINANCIERE_ID
- am not sure you got the data set correctly to begin with, you may want to check that first, that said
assuming ANNEE_FINANCIERE_ID
is the value field and CLASSEMENT
is the text field, you can set the default value by passing in the default data var default = new {ANNEE_FINANCIERE_ID=0, CLASSEMENT="default"}
as the view data from the controller to the view.
html.Kendo().ComboBox()
.Name("AnneeFinanciere")
.Placeholder("Sélectionner une année financière")
.NoDataTemplate("Aucune donnée")
.DataTextField("CLASSEMENT")
.DataValueField("ANNEE_FINANCIERE_ID")
.AutoBind(true)
.Suggest(true)
.DataSource(source =>
{
source.Read(read =>
{
read.Action("ListeAnneeFinanciere", "Operation");
})
.ServerFiltering(true);
})
.Value(ViewData["defaultClassement"])
.HtmlAttributes(new { @style = "width:100%" })
.Render();