gpt4 book ai didi

c# - MVC 后操作 ViewModel 返回为 NULL

转载 作者:行者123 更新时间:2023-11-30 21:31:42 25 4
gpt4 key购买 nike

我正在尝试将表单值发送回 Controller 。但在操作中,我将 ViewModel 设为 null。这是 View 模型

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; }
}

下面是 Controller Action :

[HttpPost]
public ActionResult Save(CommunicationDetailsViewModel communicationDetailsViewModel)
{
return View();
}

它与 MVC 的 Kendo UI 有什么关系吗?因为这是我第一次使用 Kendo UI。下面是 View :

    @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 通过输入标签的名称属性绑定(bind)属性,因此不要更改它

例如你使用

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

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

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

关于c# - MVC 后操作 ViewModel 返回为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52964256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com