簡體   English   中英

ASP.NET MVC4-在[HttpPost]創建期間,由於某種原因傳遞給Create(MyObj obj)的對象(模型)為null

[英]ASP.NET MVC4 - During an [HttpPost] Create, the object (model) that's passed in to Create(MyObj obj) is null for some reason

當我單擊“添加部門”時,它將帶我到/Department/Create?contactId=1

在此處輸入圖片說明

這是“創建”視圖。 我填寫它,然后單擊“創建”:

在此處輸入圖片說明

這是單擊“創建”后逐步執行的代碼。 請注意,該對象為null:

在此處輸入圖片說明

但是,“ contactId”不為null。 該值是“ 1”,如查詢字符串所期望的那樣。 這是我的創建視圖:

@model CallCOP.Models.Department

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    @Html.HiddenFor(model => model.dept_id)
    @Html.HiddenFor(model => model.contact_id)

    <fieldset>
        <legend>Department</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.dept)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.dept)
            @Html.ValidationMessageFor(model => model.dept)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.dept_phone)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.dept_phone)
            @Html.ValidationMessageFor(model => model.dept_phone)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

關於我在做什么錯的任何想法嗎?

更新

這是我的系課:

namespace CallCOP.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    public partial class Department
    {
        public int dept_id { get; set; }
        public int contact_id { get; set; }

        [DisplayName("Department")]
        public string dept { get; set; }

        [DisplayName("Phone Number")]
        public string dept_phone { get; set; }

        public virtual Contact Contact { get; set; }
    }
}

並且,這是根據請求為我的“創建”視圖呈現的HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Create - My ASP.NET MVC Application</title>
        <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        <link href="/Content/site.css" rel="stylesheet"/>

        <script src="/Scripts/modernizr-2.6.2.js"></script>

    </head>
    <body>
        <header>
            <div class="content-wrapper">
                <div class="float-left">
                    <p class="site-title"><a href="/">your logo here</a></p>
                </div>
                <div class="float-right">
                    <section id="login">
                       Hello, <span class="username">DOMAIN\userId</span>!
                    </section>
                    <nav>
                        <ul id="menu">
                            <li><a href="/">Home</a></li>
                            <li><a href="/Home/About">About</a></li>
                            <li><a href="/Home/Contact">Contact</a></li>
                        </ul>
                    </nav>
                </div>
            </div>
        </header>
        <div id="body">

            <section class="content-wrapper main-content clear-fix">


<form action="/Department/Create?contactId=1" method="post"><input name="__RequestVerificationToken" type="hidden" value="Q2GiIaT-qLIXxW_WN0o6o_5Tt5q2W6uXx6Ax2WEliVI2BqzAWFMZ7zwW0wX_R-rESzUn8mqFBChd8NB3jrZthK3d6LmmuRZQsdVu0v8hvSd_3_FstePrKvtjPXQ3BHtp3FXuqXdpduTsv2Nwzd1VI2cE16k1sq6QgL8ghP9HmXM1" /><input data-val="true" data-val-number="The field dept_id must be a number." data-val-required="The dept_id field is required." id="dept_id" name="dept_id" type="hidden" value="" /><input data-val="true" data-val-number="The field contact_id must be a number." data-val-required="The contact_id field is required." id="contact_id" name="contact_id" type="hidden" value="" />    <fieldset>
        <legend>Department</legend>

        <div class="editor-label">
            <label for="dept">Department</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" id="dept" name="dept" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="dept" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="dept_phone">Phone Number</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" id="dept_phone" name="dept_phone" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="dept_phone" data-valmsg-replace="true"></span>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
</form>
<div>
    <a href="/Department">Back to List</a>
</div>


            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>&copy; 2013 - My ASP.NET MVC Application</p>
                </div>
            </div>
        </footer>

        <script src="/Scripts/jquery-1.8.2.js"></script>


    <script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>


    </body>
</html>

嘗試在ActionResult更改參數的名稱:

public ActionResult Create(Department dept) {
    ...
}

public ActionResult Create(Department department) {
    ...
}

綁定與與模型上的dept屬性名稱匹配的dept參數混淆了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM