我正在使用带有剑道的MVC。单击kendo网格的添加按钮即会打开一个弹出对话框。模态对话框包含一个包含两个组合框的字段。当用户在这些字段中输入值并单击添加按钮时,我会填充Viewmodel。我使用GetLanguages()和GetCountries()操作方法填充组合框。因此,使用viewmodel不会填充这两个combox。我在视图中设置了viewmodel的模型字段。例如
@using System.Collections
@model CC.GRP.MCRequest.ViewModels.UserProfileViewModel
@(Html.Kendo().ComboBoxFor(model => model.DefaultLanguageCode)
选择组合框并不会将值添加到viewmodel。如果我做错了,有人可以告诉我。我是否需要在视图模型中添加国家/地区列表集,并在将其发送到视图之前在控制器内的viewmodel中填充集合。如果是这样的话,我该怎么做。以下是我的代码
<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填充viewmodel
控制器
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;
}
存储库
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());
}
}
屏幕
答案 0 :(得分:1)
尝试删除放在组合框中的名称,它可能会阻止 DefaultLanguageCode 正确映射到viewModel属性,因为你调用了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))
)
)