ASP.NET MVC立即更新数据表的字段
我有这个视图,我需要根据Value列中选择的值更新表任务。视图接受@model IEnumerable,然后迭代任务列表并在表中显示它们 景色ASP.NET MVC立即更新数据表的字段,asp.net,model-view-controller,Asp.net,Model View Controller,我有这个视图,我需要根据Value列中选择的值更新表任务。视图接受@model IEnumerable,然后迭代任务列表并在表中显示它们 景色 @model IEnumerable<Task> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <hr /> @Html.ValidationSumm
@model IEnumerable<Task>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<table class="table table-striped table-hover table-bordered">
<thead>
<tr class="info">
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Value)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
@Html.HiddenFor(modelItem => item.ID)
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.EditorFor(modelItem => item.Value, new { @class = "form-control" })
@Html.ValidationMessageFor(modelItem => item.Value, "", new { @class = "text-danger" })
</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
@model IEnumerable
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@DisplayNameFor(model=>model.Name)
@DisplayNameFor(model=>model.Description)
@DisplayNameFor(model=>model.Value)
@foreach(模型中的var项目)
{
@Html.HiddenFor(modeleItem=>item.ID)
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.Description)
@EditorFor(modeleItem=>item.Value,新的{@class=“form control”})
@Html.ValidationMessageFor(modelItem=>item.Value,“,new{@class=“text danger”})
}
}
控制器具有post方法估计值
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EstimateValue(IEnumerable<Task> TaskList)
{
if (ModelState.IsValid)
{
foreach (var task in TaskList)
{
db.Entry(task).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Index");
}
//1. Rebound List to view
var TaskList = db.Tasks.ToList();
//2, return model to view
return View(TaskList);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果估计值(IEnumerable任务列表)
{
if(ModelState.IsValid)
{
foreach(任务列表中的var任务)
{
db.Entry(task).State=EntityState.Modified;
db.SaveChanges();
}
返回操作(“索引”);
}
//1.要查看的反弹列表
var TaskList=db.Tasks.ToList();
//2、将模型返回到视图
返回视图(任务列表);
}
运行应用程序时,任务列表为空,我无法更新表任务。我已经使用JSON将数据作为数组从视图中发送,但TaskList的值仍然为null将IEnumerable更改为List,因为IEnumerable不允许索引。使用@for循环代替@foreach循环。所以你的观点应该是这样的:
@model List<Task>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<table class="table table-striped table-hover table-bordered">
<thead>
<tr class="info">
<th>
@Html.DisplayNameFor(model => Model[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => Model[0].Description)
</th>
<th>
@Html.DisplayNameFor(model => Model[0].Value)
</th>
</tr>
</thead>
<tbody>
@for(int i=0;i<Model.Count;i++)
{
<tr>
@Html.HiddenFor(modelItem => modelItem[i].ID)
<td>
@Html.DisplayFor(modelItem => modelItem[i].Name)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem[i].Description)
</td>
<td>
@Html.EditorFor(modelItem => modelItem[i].Value, new { @class = "form-control" })
@Html.ValidationMessageFor(modelItem => modelItem[i].Value, "", new { @class = "text-danger" })
</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
@型号列表
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.DisplayNameFor(模型=>model[0].Name)
@DisplayNameFor(模型=>模型[0]。说明)
@DisplayNameFor(模型=>模型[0]。值)
@对于(int i=0;i modelItem[i].ID)
@DisplayFor(modelItem=>modelItem[i].Name)
@DisplayFor(modelItem=>modelItem[i]。说明)
@EditorFor(modeleItem=>modeleItem[i].Value,新的{@class=“form control”})
@Html.ValidationMessageFor(modelItem=>modelItem[i].Value,“,new{@class=“text danger”})
}
}
然后更改控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EstimateValue(List<Task> TaskList)
{
if (ModelState.IsValid)
{
for(int i=0;i<TaskList.Count;i++)
{
db.Entry(TaskList[i]).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Index");
}
//1. Rebound List to view
var TaskList = db.Tasks.ToList();
//2, return model to view
return View(TaskList);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果估计值(列表任务列表)
{
if(ModelState.IsValid)
{
for(int i=0;i将IEnumerable更改为List,因为IEnumerable不允许索引。使用@foreach循环代替@foreach循环。因此您的视图应如下所示:
@model List<Task>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<table class="table table-striped table-hover table-bordered">
<thead>
<tr class="info">
<th>
@Html.DisplayNameFor(model => Model[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => Model[0].Description)
</th>
<th>
@Html.DisplayNameFor(model => Model[0].Value)
</th>
</tr>
</thead>
<tbody>
@for(int i=0;i<Model.Count;i++)
{
<tr>
@Html.HiddenFor(modelItem => modelItem[i].ID)
<td>
@Html.DisplayFor(modelItem => modelItem[i].Name)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem[i].Description)
</td>
<td>
@Html.EditorFor(modelItem => modelItem[i].Value, new { @class = "form-control" })
@Html.ValidationMessageFor(modelItem => modelItem[i].Value, "", new { @class = "text-danger" })
</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
@型号列表
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.DisplayNameFor(模型=>model[0].Name)
@DisplayNameFor(模型=>模型[0]。说明)
@DisplayNameFor(模型=>模型[0]。值)
@对于(int i=0;i modelItem[i].ID)
@DisplayFor(modelItem=>modelItem[i].Name)
@DisplayFor(modelItem=>modelItem[i]。说明)
@EditorFor(modeleItem=>modeleItem[i].Value,新的{@class=“form control”})
@Html.ValidationMessageFor(modelItem=>modelItem[i].Value,“,new{@class=“text danger”})
}
}
然后更改控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EstimateValue(List<Task> TaskList)
{
if (ModelState.IsValid)
{
for(int i=0;i<TaskList.Count;i++)
{
db.Entry(TaskList[i]).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Index");
}
//1. Rebound List to view
var TaskList = db.Tasks.ToList();
//2, return model to view
return View(TaskList);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果估计值(列表任务列表)
{
if(ModelState.IsValid)
{
for(int i=0;我感谢你,在视图中更改为List和for循环后,它工作了。再次感谢你的提问,为什么要使用forloop而不是foreach?不会吧