MVC:为什么我的Edit控制器方法没有收到ViewModel的ID? [英] MVC: Why is my Edit controller method not receiving my ViewModel's ID?

查看:51
本文介绍了MVC:为什么我的Edit控制器方法没有收到ViewModel的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC为ViewModel创建标准的创建/编辑表单.我的创建工作非常好,现在进入编辑表单,并且即使我可以看到ID设置为10004的DOM元素浏览器(和Web UI),ViewModel的ID也被传递为0.将所有字段都绑定到我的[HttpPost] Edit控制器方法,但是ID仍然为0.有什么想法吗?

I am using MVC to create standard create / edit forms for my ViewModel. I have my create working great, now I'm onto my edit form, and the ViewModel's ID is being passed as 0, even though I can see the DOM Element explorer (& web UI) that ID is set to 10004. I tried binding all the fields to my [HttpPost] Edit controller method, but the ID remains 0. Any ideas?

这是我的代码的一部分(我的字段太多,无法发布所有内容,所有其他字段都正确显示).

Here's a snippet of my code (I have too many fields, etc to post everything, and all other fields are coming across correctly).

提前谢谢!

ViewModel

public class Header_VM
    {           
        [DisplayName("Contract ID")]
        public int Contract_Key { get; set; }

        etc
    }

查看

@model (root).Header.Header_VM

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit Header: @Model.Name</h2>

@using (Html.BeginForm("Edit", "Header", FormMethod.Post, new { name = "EditForm" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal" ng-app="HeaderApp">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.Contract_Key, htmlAttributes: new { @class = "control-label col-md-2" })
            @Html.DisplayFor(model => model.Contract_Key)
        </div>
        etc...
    </div>
}

我在"int NewID ..."上有一个断点,那时Contract_Key为0.

I have a break point on "int NewID...", and at that point Contract_Key is 0.

控制器

[HttpPost]
public ActionResult Edit([Bind(Include = "Contract_Key, etc...")] Header_VM header)
{
     int NewID = DB.Header_Insert(header);
     return RedirectToAction("Details", "Header", new { Contract_Key = NewID });
 }

推荐答案

DisplayFor 帮助器方法生成HTML标记以显示该属性值.因此,如果您检查页面源(查看源),您将看到Contract_Key属性的 label 标签的标记.提交表单时,仅提交输入表单字段值,而不提交标签/div值

The DisplayFor helper method generates HTML markup to display the property value. So if you inspect the page source(view source), you will see markup for a label tag for the Contract_Key property. When you submit the form, only input form field values are submitted , not label/div values

您需要在表单内部输入一个表单字段,其名称属性值设置为"Contract_Key" .

You need an input form field inside your form with name attribute value set to "Contract_Key".

您可以使用 HiddenFor 帮助器方法,该方法将生成一个隐藏的输入元素,其名称属性值与属性名称匹配.

You can use the HiddenFor helper method, which generates a hidden input element with the name attribute value matching to the property name.

 @Html.DisplayFor(model => model.Contract_Key)
 @Html.HiddenFor(model => model.Contract_Key)

或者仅仅是手写的隐藏输入

Or simply a hand written hidden input

<input type="hidden" name="Contract_Key" value="@Model.Contract_Key" />

这篇关于MVC:为什么我的Edit控制器方法没有收到ViewModel的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆