【发布时间】:2018-10-24 08:28:20
【问题描述】:
我正在尝试将表单值发布回控制器。但在行动中,我将 ViewModel 设为空。 这是视图模型
public class CommunicationDetailsViewModel
{
public string OrganizationName { get; set; }
public List<Country> Country { get; set; }
public List<State> State { get; set; }
public List<City> City { get; set; }
[Display(Name = "Id")]
public int CountryId { get; set; }
[Display(Name = "Id")]
public int StateId { get; set; }
[Display(Name = "Id")]
public int CityId { get; set; }
[StringLength(32), Required(ErrorMessage ="Address is required")]
public string Address { get; set; }
[StringLength(32), Required(ErrorMessage = "Building name is required")]
public string BuildingName { get; set; }
}
下面是控制器动作:
[HttpPost]
public ActionResult Save(CommunicationDetailsViewModel communicationDetailsViewModel)
{
return View();
}
它与 MVC 的 Kendo UI 有什么关系吗?因为这是我第一次使用 Kendo UI。下面是视图:
@model WebAPI.ViewModels.CommunicationDetailsViewModel
@{
ViewBag.Title = "Supplier Information";
}
<h4>Supplier Details</h4>
@using (Html.BeginForm("Save", "SupplierInformation", FormMethod.Post ))
{
<div class="demo-section k-content">
<div class="form-group">
@Html.Label("Organization name")
@Html.Kendo().TextBoxFor(model => model.OrganizationName).Name("txtOrganization").HtmlAttributes(new { @class = "k-textbox required", placeholder = "Organization Name" })
</div>
<div class="form-group">
@Html.Label("Country")
@(Html.Kendo().DropDownList().Name("ddlCountry").DataTextField("CountryName").DataValueField("Id").BindTo(Model.Country))
</div>
<div class="form-group">
@Html.Label("State")
@(Html.Kendo().DropDownList().Name("ddlState").DataTextField("StateName").DataValueField("Id").BindTo(Model.State))
</div>
<div class="form-group">
@Html.Label("City")
@(Html.Kendo().DropDownList().Name("ddlCity").DataTextField("CityName").DataValueField("Id").BindTo(Model.City))
</div>
<div class="form-group">
@Html.Label("Address")
@Html.Kendo().TextBoxFor(model => model.Address).Name("txtAddress").HtmlAttributes(new { @class="k-textbox required", placeholder="Address", @maxlength = "32" })
</div>
<div class="form-group">
@Html.Label("Building name")
@Html.Kendo().TextBoxFor(model => Model.BuildingName).Name("txtBuildingName").HtmlAttributes(new { @class = "k-textbox required", placeholder = "Address", @maxlength = "32" })
</div>
</div>
@Html.Kendo().Button().Name("btnSave").Content("Save").HtmlAttributes(new { type = "submit", @class = "k-button k-primary" })
}
有趣的是,如果我使用 FormCollection 而不是我的 ViewModel,我可以获取操作中的值。
我在这里想念什么?一定是什么蠢事。任何帮助表示赞赏。
【问题讨论】:
-
类似问题:telerik.com/forums/editorfor-with-the-name-method-returns-null => 如果您使用
...For()助手,只需删除Name()函数。
标签: c# kendo-ui kendo-asp.net-mvc