1

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

当我单击“创建”actionLink 时,生成的视图与设计的一样。所以我用适当的值填充字段来测试行为。

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

这是处理 Create 的控制器方法:

[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()
        {

        }
    }
}
4

2 回答 2

3

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

public string m_Address { get; set; }
于 2013-02-19T20:06:58.110 回答
2
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;};
于 2013-02-19T20:10:35.083 回答