【问题标题】:Asp.net MVC 5 - View Posting Null Data to ControllerAsp.net MVC 5 - 查看将空数据发布到控制器
【发布时间】:2018-08-01 14:52:00
【问题描述】:

我正在开发一个使用视图模型将数据传递给视图的应用程序,但是当我尝试保存用户对数据所做的更改并将其发布回控制器以进行处理并存储在数据库中时,我的视图模型中的所有内容都为空值。我在这里尝试了各种其他问题,但我找到的答案都无法解决这个问题。我认为这与我使用 ListBox 来显示一些数据有关,但我对 ASP.net MVC 5 还很陌生,不太确定我需要在这里做什么。

下面是我的视图模型、控制器的相关部分和视图的代码。

查看模型

public class OrganizationViewModel
{
    public Organization Organization { get; set; }
    public IEnumerable<SelectListItem> RelatedContacts { get; set; }
}

将数据从控制器发送到视图

[HttpGet]
    public ActionResult Edit(Organization organization)
    {
        IEnumerable<SelectListItem> contactsList = GetContacts(organization);
        var viewModel = new OrganizationViewModel()
        {
            Organization = organization,
            RelatedContacts = contactsList
        };

        return View("Edit", viewModel);
    }

从视图接收数据

[HttpPost]
    public ActionResult SaveOrganization(OrganizationViewModel organizationViewModel)
    {
        organizationViewModel.Organization.Id = (int)TempData["Id"];
        organizationViewModel.Organization.LastEdited = DateTime.Today;
        organizationViewModel.Organization.DateAdded = (DateTime)TempData["DateAdded"];
        TempData.Clear();
        if (ModelState.IsValid)
        {
            organizationRepository.SaveOrganization(organizationViewModel.Organization);
            return RedirectToAction("Index", "Organization");
        }

        IEnumerable<SelectListItem> contactsList = GetContacts(organizationViewModel.Organization);
        var viewModel = new OrganizationViewModel()
        {
            Organization = organizationViewModel.Organization,
            RelatedContacts = contactsList
        };
        return View("Edit", viewModel);
    }

编辑视图

<div class="panel">
<div class="panel-heading">
    <h3>Editing @(Model.Organization.Name)</h3>
</div>

@using (Html.BeginForm("SaveOrganization", "Organization", FormMethod.Post))
{
    <div class="panel-body">
        @Html.HiddenFor(m => Model.Organization.Name)
        <div class="row">
            <div class="col-md-3 form-group">
                <label>Organization Name</label>
                @Html.TextBox("Name", null, new { @class = "form-control" })
            </div>
            <div class="col-md-3 form-group">
                <label>Address</label>
                @Html.TextArea("Address", null, new { @class = "form-control" })
            </div>
        </div>
        <div class="row">
            <div class="col-md-3 form-group">
                <label>Related Contacts</label>
                @Html.ListBox("RelatedContacts", Model.RelatedContacts, new { @class = "form-control", size = 10 })
            </div>
            <div class="col-md-3 form-group">
                <label>Custom Fields</label>
                <br />
                <input type="button" value="Add New Field" class="btn btn-default" />
            </div>
        </div>
        <div class="row">
            <div class="col-md-12 form-group">
                <label>Notes</label>
                @Html.TextArea("Notes", null, 10, 500, new { @class = "form-control", @style = "width: 100%; max-width: 100%;" })
            </div>
        </div>
    </div>
    <div class="panel-footer">
        <input type="submit" value="Save" class="btn btn-primary" />
        @Html.ActionLink("Cancel and return to List", "CancelEditOrAdd", null, new { @class = "btn btn-default" })
    </div>
}

【问题讨论】:

  • 您在表单控件中生成的name 属性与您的模型完全没有关系。始终使用强类型方法 - @Html.TextBoxFor(m =&gt; m.Organisation.Name) - 然后比较两种情况下的 name 属性,以了解您的代码为何不起作用。
  • 但视图模型在编辑数据时不包含数据模型。它们包含您在视图中需要的数据模型的属性。参考What is ViewModel in MVC?
  • 谢谢!解决了问题的一部分——我现在在回发时在视图模型中获取组织的数据,但对于相关联系人来说仍然没有。目前,除了显示数据之外,我没有使用 ListBox 做任何事情,最终我将在以后的实现中使用其中的选择。
  • 你还没有展示你的模型,或者它的属性是什么!并且 &lt;select multiple&gt; 不能绑定到集合或复杂对象 - 它回发一组简单属性(因此您的绑定 tp 将是(比如说)IEnumerable&lt;int&gt; SelectedContacts
  • 你需要做的最重要的事情是改变你的视图模型。它将不包含您的 Organization Organization 属性。相反,它将包含您编辑的Organization 的每个属性的属性,以及IEnumerable&lt;SelectListItem&gt; 属性)

标签: c# html asp.net-mvc razor asp.net-mvc-5


【解决方案1】:

为偶然发现此问题并希望查看我根据@Stephen Muecke 的 cmets 所做的确切更改的任何人回答我自己的问题。

将所有@html.something()s 更改为@html.somethingfor()s 以解决在视图模型中未收到组织的回发数据的问题,然后我在视图模型中创建了一个新属性以与列表框中的选定索引相对应,现在为@Html.ListBoxFor(m =&gt; m.SelectedIndexes, Model.RelatedContacts, ...)。我现在正在获取所有正确的回发数据,并且可以将所选索引与组织的相关联系人联系起来。

编辑:还更改了我的视图模型以包含我的组织类的属性,而不是将实际的组织对象本身作为属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多