没有数据传递到“;创建“;视图对象

本文关键字:创建 视图 对象 数据 | 更新日期: 2023-09-27 18:00:20

我的MVC应用程序有一个我不理解的奇怪行为。

当我点击"创建"actionLink时,生成的视图与设计的一样。因此,我在字段中填充适当的值来测试行为。

但是当我点击"创建"按钮时,传递给受控对象的所有字段都为null,id为0,我真的不明白它为什么这么做

以下是处理创建的控制器方法:

[HttpPost]
public ActionResult Create(ObjInfo _obj)
{
    if (ModelState.IsValid)
    {
        m_ObjManager.CreateObj(_obj);
        return RedirectToAction("Index");
    }
    return View(_obj);
}

以下是我如何编码我的"创建"视图:

@model MyApp.Models.ObjInfo
@{
    ViewBag.Title = "Create";
}
<h2>Create New Object</h2>
@using (Html.BeginForm()){
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>OBJ</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.m_ID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_ID)
            @Html.ValidationMessageFor(model => model.m_ID)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.m_Address)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_Address)
            @Html.ValidationMessageFor(model => model.m_Address)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.m_City)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_City)
            @Html.ValidationMessageFor(model => model.m_City)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.m_ContactName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_ContactName)
            @Html.ValidationMessageFor(model => model.m_ContactName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.m_ContactTitle)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_ContactTitle)
            @Html.ValidationMessageFor(model => model.m_ContactTitle)
        </div>
        (...)
        <p>
            <input type="submit" value="Create"/>
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to Link", "Index")
</div>
@section scripts{
    @Scripts.Render("~/bundles/jqueryval")
}

如果需要,我准备提供我的代码来解决这个问题。

namespace MyApp.Models 
{
    public class ObjInfo 
    {
        /// <summary>
        /// Properties declaration.
        /// </summary>
        public int m_ID;
        public string m_Address;
        public string m_City;
        public string m_ContactName;
        public string m_ContactTitle;
        public string m_Country;
        public string m_Name;
        public string m_Email;
        public string m_FaxNumber;
        public string m_Notes;
        public string m_PhoneNumber;
        public string m_PostalCode;
        public string m_Region;
        /// <summary>
        /// Default Constructor.
        /// </summary>
        public CustomerInfo()
        {
        }
        /// <summary>
        /// Overloaded constructor with parameters.
        /// </summary>
        /// <param name="_adress"></param>
        /// <param name="_city"></param>
        /// <param name="_contactName"></param>
        /// <param name="_contactTitle"></param>
        /// <param name="_country"></param>
        /// <param name="_customerID"></param>
        /// <param name="_name"></param>
        /// <param name="_mail"></param>
        /// <param name="_faxNumber"></param>
        /// <param name="_notes"></param>
        /// <param name="_phoneNumber"></param>
        /// <param name="_postalCode"></param>
        /// <param name="_region"></param>
        public CustomerInfo(string _adress, string _city, string _contactName, string _contactTitle, string _country, int _customerID, string _customerName,
            string _mail, string _faxNumber, string _notes, string _phoneNumber, string _postalCode, string _region)
        {
            this.m_Address = _adress;
            this.m_City = _city;
            this.m_ContactName = _contactName;
            this.m_ContactTitle = _contactTitle;
            this.m_Country = _country;
            this.m_CustomerID = _customerID;
            this.m_CustomerName = _customerName;
            this.m_EbayCustomer = _ebayCustomer;
            this.m_Email = _mail;
            this.m_FaxNumber = _faxNumber;
            this.m_Notes = _notes;
            this.m_PhoneNumber = _phoneNumber;
            this.m_PostalCode = _postalCode;
            this.m_Region = _region;
        }
        /// <summary>
        /// Destructor.
        /// </summary>
        ~CustomerInfo()
        {
        }
        /// <summary>
        /// Dispose method.
        /// </summary>
        public virtual void Dispose()
        {
        }
    }
}

没有数据传递到“;创建“;视图对象

确保您的模型类在每个属性上都有公共setter,即

public string m_Address { get; set; }
namespace MyApp.Models 
{
    public class ObjInfo 
    {
        /// <summary>
        /// Properties declaration.
        /// </summary>
        public int m_ID {get; set;};
        public string m_Address {get; set;};
        public string m_City {get; set;};
        public string m_ContactName {get; set;};
        public string m_ContactTitle {get; set;};
        public string m_Country {get; set;};
        public string m_Name {get; set;};
        public string m_Email {get; set;};
        public string m_FaxNumber {get; set;};
        public string m_Notes {get; set;};
        public string m_PhoneNumber {get; set;};
        public string m_PostalCode {get; set;};
        public string m_Region {get; set;};