Reputation: 8701
I am using MVC with kendo. A popup dialog opens on the click of the add button of the kendo grid. The modal dialog contains a fields including two comboboxes. I am populating the Viewmodel when the user enters values in those fields and clicks the add button. I am populating the combo boxes using the GetLanguages() and GetCountries() action methods. So these two comboxes are not populated using the viewmodel. I have set the model field of my viewmodel in the view. For e.g
@using System.Collections
@model CC.GRP.MCRequest.ViewModels.UserProfileViewModel
@(Html.Kendo().ComboBoxFor(model => model.DefaultLanguageCode)
Selecting the comboboxes doesnt add the value to the viewmodel. Could someone let me know if I am doing something wrong. Do I need to add the listcollection of countries in my view model and populate the collections in my viewmodel within the controller before sending it to the view. If that is the case how do i do it. Below is my code
<div class="editor-label">
@Html.LabelFor(model => model.Forename)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Forename)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Surname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Surname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PreferredName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PreferredName)
</div>
<div class="editor-field">
@(Html.Kendo().ComboBoxFor(model => model.DefaultLanguageCode)
// .Name("LanguageCode")
.HtmlAttributes(new { style = "width:300px" })
.DataTextField("LanguageName")
.DataValueField("DefaultLanguageCode")
.DataSource(dataSource => dataSource
.Read(read => read.Action("GetLanguages", "Admin").Type(HttpVerbs.Post))
)
)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DefaultCountryCode)
</div>
<div class="editor-field">
@(Html.Kendo().ComboBoxFor(model => model.DefaultCountryCode)
// .Name("CountryCode")
.HtmlAttributes(new { style = "width:300px" })
.DataTextField("Country")
.DataValueField("DefaultCountryCode")
.DataSource(dataSource => dataSource
.Read(read => read.Action("GetCountries", "Admin").Type(HttpVerbs.Post))
)
)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TimeZoneName)
</div>
<div class="editor-field">
@(Html.Kendo().ComboBoxFor(model => model.TimeZoneName)
// .Name("TimeZoneID")
.HtmlAttributes(new { style = "width:300px" })
.DataTextField("TimeZoneName")
.DataValueField("TimeZoneID")
.DataSource(dataSource => dataSource
.Read(read => read.Action("GetTimeZones", "Admin").Type(HttpVerbs.Post))
)
)
</div>
UserProfile_Read populates the viewmodel
Controller
public ActionResult GetLanguages()
{
return Json(MCRHelper.GetLanguages(), JsonRequestBehavior.AllowGet);
}
public ActionResult GetCountries()
{
return Json(MCRHelper.GetCountries(), JsonRequestBehavior.AllowGet);
}
public ActionResult UserProfile_Read([DataSourceRequest]DataSourceRequest request)
{
var users = mcrRepository.GetUserProfileById(0).AsQueryable().ProjectTo<UserProfileViewModel>();
var response = users.ToDataSourceResult(request);
return Json(response, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult UserProfile_Create([DataSourceRequest] DataSourceRequest request, UserProfileViewModel userVM)
{
if (!ModelState.IsValid)
{
return null;
}
if (userVM.UserProfileID == 0)
{
mcrRepository.CreateUserProfile(Mapper.Map<UserProfile>(userVM));
return Json(mcrRepository.GetTeams().ToDataSourceResult(request));
}
else
{
mcrRepository.UpdateUserProfile(Mapper.Map<UserProfile>(userVM));
}
return null;
}
Repository
public void CreateUserProfile(UserProfile userProfile)
{
if (MCRHelper.UserValidate() == 1)
{
var userProfiles = db.spInsertUserProfile(userProfile.EmployeeID,
userProfile.Forename,
userProfile.Surname,
userProfile.PreferredName,
userProfile.DefaultLanguageCode,
userProfile.DefaultCountryCode,
userProfile.TimeZoneID,
userProfile.Domain,
userProfile.NetworkID,
userProfile.EmailAddress,
true,
MCRHelper.GetShortname());
}
}
Screen
Upvotes: 0
Views: 86
Reputation: 441
Try to remove the name you put on the combobox it may prevent DefaultLanguageCode to be correctly mapped to the viewModel attribute, Because you called you combobox LanguageDD.
@(Html.Kendo().ComboBoxFor(model => model.DefaultLanguageCode)
//.Name("LanguageDD") <-- here
.HtmlAttributes(new { style = "width:300px" })
.DataTextField("LanguageName")
.DataValueField("LanguageCode")
.DataSource(dataSource => dataSource
.Read(read => read.Action("GetLanguages", "Admin").Type(HttpVerbs.Post))
)
)
Upvotes: 1