MVC post 操作 ViewModel 返回为 NULL

MVC post action ViewModel is returned as NULL

我正在尝试 post 将表单值返回给控制器。但在操作中,我将 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();
}

它与 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,我就能够在操作中获取值。
我在这里错过了什么?一定是愚蠢的东西。任何帮助表示赞赏。

我认为这里的问题是由于您通过 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" })