gpt4 book ai didi

c# - MVC 模型在 HTTPOST 中为空

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

关于这个问题已经有很多问题了,但是尽管查看了其中的一些问题,我还是很难看出问题出在哪里。当我提交以下表格时,模型为空。关于我在这里做错了什么有什么建议吗?

模型(部分):

public class ItemHeader
{
public string hdr_contact_name { get; set; }
public string hdr_contact_method { get; set; }
}

Controller :

   public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.NotFound);
}

int id2 = (int)id;
DB db = new DB();
ItemHeader itemHeader = db.GetHeader(id2);
if (itemHeader == null)
{
return new HttpStatusCodeResult(HttpStatusCode.NotFound);
}
else
{
return View(itemHeader);
}
}
[HttpPost]
public ActionResult Details(ItemHeader itemHeader)
{
string test;
DB db = new DB();
Response.Write(db.UpdateHeader(itemHeader));
Response.End();
return null;
}

查看(部分):

@model MVCTest2.Models.ItemHeader
@{ Html.EnableClientValidation(true); }
@using (Html.BeginForm())
{
<table>
<tr>
<td>Vendor Contact</td>
<td>@Html.TextBoxFor(model => model.hdr_contact_name, new { style = "width:24em", maxlength = 40 })</td>
<td>Email/Phone</td>
<td>@Html.TextBoxFor(model => model.hdr_contact_method, new { style = "width:24em", maxlength = 40 })</td>
</tr>
</table>
<input type="submit" value="Submit" />
}

HTML(部分):

<div>
<form action="/MVCTest2/VendorItem/Details/69" method="post">
<h4>Item Request Details</h4>
<hr />
<table>
<tr>
<td>Vendor Contact</td>
<td><input id="hdr_contact_name" maxlength="40" name="hdr_contact_name" style="width:24em" type="text" value="" /></td>
<td>Email/Phone</td>
<td><input id="hdr_contact_method" maxlength="40" name="hdr_contact_method" style="width:24em" type="text" value="" /></td>
</tr>
</table>
<p>
<input type="submit" value="Submit" />
&nbsp;&nbsp;
</p>
</form></div>

最佳答案

表单正在发布到 /MVCTest2/VendorItem/Details/69 但操作方法 POST Details 没有 id 参数。

尝试使用这个代替你实际拥有的:

[HttpPost]
public ActionResult Details(int? id, ItemHeader itemHeader)
{
string test;
DB db = new DB();
Response.Write(db.UpdateHeader(itemHeader));
Response.End();
return null;
}

关于c# - MVC 模型在 HTTPOST 中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26492351/

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