Mar*_*ark 4 asp.net asp.net-mvc
是否有可能有一个编辑多个记录的视图,就像index.cshtml视图循环通过记录显示它们一样(如下所示)?
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.tvid)
</td>
Run Code Online (Sandbox Code Playgroud)
因此,对于上面的每一行,它将与数据库中的不同行相关.
有谁知道有任何例子说明如何实现这一目标?
谢谢你的任何指示,
标记
UPDATE
模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcObjectives.Models
{
public class objectives
{
public int ID { get; set; }
public int tvid { get; set; }
public string tlnt { get; set; }
public DateTime month { get; set; }
public string objective { get; set; }
public int score { get; set; }
public int possscore { get; set; }
public string comments { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
}
控制器:
[HttpPost]
public ActionResult Edit(objectives objectives)
{
if (ModelState.IsValid)
{
db.Entry(objectives).State = EntityState.Modified;
foreach (objective Objective in objectives.objective)
{ }
db.SaveChanges();
return RedirectToAction("Index");
}
return View(objectives);
}
Run Code Online (Sandbox Code Playgroud)
如果有人能提供任何帮助,我会坚持使用控制器吗?
再次感谢,
标记
第二次更新
将记录发送到视图的GET控制器是:
// GET: /Objective/Edit/
public ActionResult Edit()
{
return View(db.objectives.ToList());
}
Run Code Online (Sandbox Code Playgroud)
POST控制器(从视图中回发值的位置)是:
// POST: /Objective/Edit/
[HttpPost]
public ActionResult Edit(List<objectives> objectives)
{
if (ModelState.IsValid)
{
// the next part is where I am stuck - how to loop through the returned objectives, and update the records in the database
db.Entry(objectives).State = EntityState.Modified;
foreach (objectives obj in objectives)
{
var tempObj = (from objv in db.objectives
where objv.ID==obj.ID
select objv).First();
}
// to do - how to save the updates sent back????
return RedirectToAction("Index");
}
return View(objectives);
}
Run Code Online (Sandbox Code Playgroud)
使用编辑器模板
假设您的ViewModel/Model看起来像这样
public class UserViewModel
{
public int UserId { set;get;}
public string Name { set;get;}
public IEnumerable<Address> Addresses { set;get;}
public UserViewModel()
{
if(this.Addresses==null)
this.Addresses=new List<Address>();
}
}
public class Address
{
public int AddressID { set;get;}
public string AddressLine1 { set;get;}
}
Run Code Online (Sandbox Code Playgroud)
现在创建一个addresses.cshtml使用以下内容调用的编辑器模板
@model YourNameSpace.Address
@Html.TextBoxFor(x => x.AddressLine1)
Run Code Online (Sandbox Code Playgroud)
在主视图中,您可以将其称为
@model UserViewModel
@using (Html.BeginForm())
{
//other elements
@Html.EditorFor(m=>m.Addresses)
<input type="submit" value="Save" />
}
Run Code Online (Sandbox Code Playgroud)
现在,您将获得HttpPost Ation方法中的数据
[HttpPost]
public ActionResult Save(UserViewModel model)
{
foreach (Address address in model.Addresses)
{
//now check for address.AddressLine here
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 :根据用户对问题的评论和更新.
(请求OP:下次发布问题时,请在第一时间将所有相关详细信息包含在问题中.)
创建一个ViewModel来包装Objective类的List.
public class ObjectivesEdit
{
public IEnumerable<Objective> Objectives { set; get; }
public ObjectivesEdit()
{
if (Objectives == null)
Objectives = new List<Objective>();
}
}
Run Code Online (Sandbox Code Playgroud)
在您的GET Action方法中,将此Wrapper View模型发送到视图,并填充值
public ActionResult Edit()
{
ObjectivesEdit objEdit = new ObjectivesEdit();
List<Objective> objList = new List<Objective>();
// you can replace this manual filling with data from database
objList.Add(new Objective { ID = 1, score = 65 });
objList.Add(new Objective { ID = 2, score = 43 });
objList.Add(new Objective { ID = 3, score = 78 });
objEdit.Objectives = objList;
return View(objEdit);
}
Run Code Online (Sandbox Code Playgroud)
您的编辑器模板应如下所示.应该命名objective.cshtml
@model EditorTemplateDemo.Models.Objective
<p>
Score for @Model.ID is @Html.TextBoxFor(x => x.score)
@Html.HiddenFor(x => x.ID)
</p>
Run Code Online (Sandbox Code Playgroud)
而你的主要观点
@model EditorTemplateDemo.Models.ObjectivesEdit
@using (Html.BeginForm())
{
@Html.EditorFor(x=>x.Objectives)
<input type="submit" value="Save" />
}
Run Code Online (Sandbox Code Playgroud)
最后,您的HTTPPOST操作方法将如下所示
[HttpPost]
public ActionResult Edit(ObjectivesEdit model)
{
if (model.Objectives != null)
{
// Put a break point here and you will see the posted data
foreach (var item in model.Objectives)
{
context.Entry(item).State = EntityState.Modified;
}
//Save and redirect
context.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
这应该工作.测试.
为了避免进一步的问题,我分享了上述代码的工作示例在这里.请在代码中使用visual studio breakpoints来查看发布的值.
| 归档时间: |
|
| 查看次数: |
9101 次 |
| 最近记录: |