C# 无法将数据从视图发送到控制器操作.NET MVC
只需将int数据从视图发送到控制器操作。但控制器操作未接收该int数据/无法映射该int数据 我的控制器=>C# 无法将数据从视图发送到控制器操作.NET MVC,c#,asp.net,asp.net-mvc,forms,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Forms,Asp.net Mvc 4,只需将int数据从视图发送到控制器操作。但控制器操作未接收该int数据/无法映射该int数据 我的控制器=> [HttpPost] [ValidateAntiForgeryToken] public ActionResult ConfirmNewBatchRequest(int? id) { if ((id?? 0) == 0) { return new HttpStatusCodeResult(HttpSt
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ConfirmNewBatchRequest(int? id)
{
if ((id?? 0) == 0)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Studentassignbatche ConfirmStudentassignbatche = db.Studentassignbatches.Find(id);
Batche FindBatche = db.Batches.Find(ConfirmStudentassignbatche.batch_code);
if (ConfirmStudentassignbatche == null)
{
return HttpNotFound();
}
ConfirmStudentassignbatche.StatusCode = 1;
db.Financeofstudents.Add(new Financeofstudent()
{
UserId = ConfirmStudentassignbatche.UserId,
batch_code = ConfirmStudentassignbatche.batch_code,
debit = FindBatche.amount,
credit = 0,
balance = FindBatche.amount,
lastTrunsaction = DateTime.Now,
entry_date = DateTime.Now
});
TryUpdateModel(ConfirmStudentassignbatche, new string[] { "StatusCode" });
db.SaveChanges();
TempData["BatchConfirmSuccess"] = "Success";
return RedirectToAction("Index");
}
而我的观点=>
@using Something
@model IEnumerable<Studentassignbatche>
@{
ViewBag.Title = "Confirm New Batch Request From Student";
IEnumerable<Course> CoursesTable = TempData["Courses"] as IEnumerable<Course>;
IEnumerable<UserDetail> UserDetailTable = TempData["UserDetails"] as IEnumerable<UserDetail>;
}
@section PageDescription{
<section class="content-header">
<h1>
@ViewBag.Title
<small>Tsms-admin</small>
</h1>
</section>
}
<div class="table-responsive">
<table class="table table-striped table-inverse table-bordered">
<tr>
<th>#</th>
<th>
@Html.DisplayNameFor(model => model.UserId)
</th>
<th>
@Html.Label("Student Name")
</th>
<th>
@Html.DisplayNameFor(model => model.batch_code)
</th>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.Label("Vendor Name")
</th>
<th>
@Html.DisplayNameFor(model => model.adding_date)
</th>
<th></th>
</tr>
@{
int counter = 1;
}
@foreach (var item in Model)
{
<tr>
<td>
@counter
</td>
<td>
@Html.DisplayFor(modelItem => item.UserId)
</td>
<td>
@{
var UserDetailsId = UserDetailTable.Where(x => x.UserId == item.UserId).Select(x => x.id).FirstOrDefault();
var name = UserDetailTable.Where(x => x.UserId == item.UserId).Select(x => x.fullname).FirstOrDefault();
}
@Html.ActionLink(name, "Details", "Students", new { id = UserDetailsId }, null)
</td>
<td>
@Html.ActionLink(item.batch_code, "Details", "Batche", new { id = item.batch_code }, null)
</td>
<td>
@Html.ActionLink(item.name, "Details", "Course", new { id = item.name }, null)
</td>
<td>
@{
var vendorname = CoursesTable.Where(x => x.name == item.name).Select(x => x.vendor_heading).FirstOrDefault();
}
<label style="font-weight:normal !important">@vendorname</label>
</td>
<td>
@Html.DisplayFor(modelItem => item.adding_date)
</td>
<td style="width:100px;">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(modelitem => item.id)
<div class="form-actions no-color">
<input type="submit" value="Confirm" class="btn btn-success" />
</div>
}
</td>
</tr>
counter++;
}
</table>
</div>
但在首次亮相时,它仍然向我显示一个空对象
之所以会发生这种情况,是因为在视图端它可以生成多个
。但据我所知,如果视图生成多个
,每个表单仍然会得到一个隐藏的输入字段。
当表单提交时,它将显示与该
关联的特定隐藏字段。或者我的URL看起来像http://localhost:43847/Admin/ConfirmNewBatchRequest
URL有问题吗?那么为什么会出现这个问题,以及如何消除这个问题。问题是表单和回发方法不匹配
在表单中,呈现的隐藏字段的名称
是项.id
。但是,您的操作方法只需要一个名为id
的int。表单将提交它,就像id
是名为item
的对象的一部分一样
用于呈现字段的HTML帮助器是为您想要回发整个模型而设计的,而不仅仅是一个字段-即,在这种情况下,您的操作方法必须接受类型为studentassignbatch
的对象
要解决此问题,可以使用更简单版本的HiddenField助手:
@Html.Hidden("id", item.id)
这应该呈现如下内容:
<input name="id" type="hidden" value="3" />
这意味着
名称
与操作方法所需的名称匹配。问题在于表单和回发方法不匹配
在表单中,呈现的隐藏字段的名称
是项.id
。但是,您的操作方法只需要一个名为id
的int。表单将提交它,就像id
是名为item
的对象的一部分一样
用于呈现字段的HTML帮助器是为您想要回发整个模型而设计的,而不仅仅是一个字段-即,在这种情况下,您的操作方法必须接受类型为studentassignbatch
的对象
要解决此问题,可以使用更简单版本的HiddenField助手:
@Html.Hidden("id", item.id)
这应该呈现如下内容:
<input name="id" type="hidden" value="3" />
这意味着
名称
与操作方法所需的名称匹配。呈现的HTML是什么样子的?您创建的隐藏字段是否确实包含正确的ID?@ADyson html看起来像==>
呈现的html看起来像什么?您创建的隐藏字段是否确实包含正确的ID?@ADyson html看起来像==>
<input name="id" type="hidden" value="3" />