私の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()
{
}
}
}