【问题标题】:MVC post action ViewModel is returned as NULLMVC 后操作 ViewModel 返回为 NULL
【发布时间】: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,我可以获取操作中的值。
我在这里想念什么?一定是什么蠢事。任何帮助表示赞赏。

【问题讨论】:

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


【解决方案1】:

我认为这里的问题是由Name 函数更改名称引起的。请注意,MVC 通过输入标签的 name 属性绑定属性,所以不要更改它

例如你使用

    @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" })

【讨论】:

  • 我真是个笨蛋。就是这样。我将控件的名称更改为属性的名称并且它起作用了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-06-18
  • 1970-01-01
  • 2015-01-27
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多