gpt4 book ai didi

c# - 没有数据传递给 "Create" View 对象

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:47 24 4
gpt4 key购买 nike

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

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

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

这是处理创建的 Controller 方法:

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

return View(_obj);
}

下面是我如何编写“创建” View 的代码:

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

}
}
}

最佳答案

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

public string m_Address { get; set; }

关于c# - 没有数据传递给 "Create" View 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14966153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com