MVC后操作ViewModel返回为NULL

时间:2018-10-24 08:28:21

标签: c# kendo-ui kendo-asp.net-mvc

我正在尝试将表单值发布回控制器。但是在操作中,我将ViewModel设置为null。 这是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();
}

它与Kendo UI for MVC有什么关系吗?因为这是我第一次使用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,则可以在操作中获取值。
我在这里想念什么?一定是愚蠢的。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

我认为这里的问题是由您通过Name函数更改名称引起的。请注意,输入标签的MVC绑定属性按名称属性,因此请勿更改

例如,您使用

    @Html.Kendo().TextBoxFor(model => model.OrganizationName).Name("txtOrganization").HtmlAttributes(new { @class = "k-textbox required", placeholder = "Organization Name" })

您将输入名称从OrganizationName更改为txtOrganization,这可能导致MVC无法完全绑定属性。您应该保留其原始名称,或忽略这样的更改名称

    @Html.Kendo().TextBoxFor(model => model.OrganizationName).Name("OrganizationName").HtmlAttributes(new { @class = "k-textbox required", placeholder = "Organization Name" })