asp.net mvc更新多条记录

时间:2012-05-29 13:44:20

标签: asp.net asp.net-mvc

是否可以有一个用于编辑多个记录的视图,就像index.cshtml视图循环遍历记录以显示它们一样(如下所示)?

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.tvid)
    </td>

因此,对于上面的每一行,它将与数据库中的不同行相关。

有没有人知道如何实现这一目标的任何例子?

感谢您的任何指示,

标记

更新

型号:

 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; }
 }

}

控制器:

 [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);
    }

如果有人可以提供帮助,我会坚持使用控制器吗?

再次感谢,

标记

第二次更新

将记录发送到视图的GET控制器是:

        // GET: /Objective/Edit/
        public ActionResult Edit()
        {
            return View(db.objectives.ToList());
        }

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);
        }

3 个答案:

答案 0 :(得分:7)

使用编辑器模板

假设您的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;}
}

现在创建一个名为addresses.cshtml的编辑器模板,其中包含以下内容。

@model YourNameSpace.Address
@Html.TextBoxFor(x => x.AddressLine1)

在主视图中,您可以将其称为

@model UserViewModel 
@using (Html.BeginForm())
{
  //other elements
 @Html.EditorFor(m=>m.Addresses)
 <input type="submit" value="Save" />
}

现在您将获得HttpPost Ation方法中的数据

[HttpPost]
public ActionResult Save(UserViewModel model)
{  
   foreach (Address address in model.Addresses)
   {
      //now check for address.AddressLine here
   } 
}

编辑:根据用户对问题的评论和更新。

请求OP 下次发布问题时,请在第一时间将所有相关详细信息包含在问题中。)

创建一个ViewModel来包装Objective类的List。

public class ObjectivesEdit
{
    public IEnumerable<Objective> Objectives { set; get; }
    public ObjectivesEdit()
    {
        if (Objectives == null)
            Objectives = new List<Objective>();
    }
}

在您的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);
  }

您的编辑器模板应如下所示。它应该命名为objective.cshtml

@model EditorTemplateDemo.Models.Objective           
<p>
Score for @Model.ID is  @Html.TextBoxFor(x => x.score)
@Html.HiddenFor(x => x.ID)
</p>

您的主视图

@model EditorTemplateDemo.Models.ObjectivesEdit
@using (Html.BeginForm())
{
  @Html.EditorFor(x=>x.Objectives)
  <input type="submit" value="Save" />    
}

最后,您的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);
    }

这应该有效。测试。

为了避免进一步的问题,我正在分享上述代码here的工作示例。请在代码中使用visual studio breakpoints来查看正在发布的值。

答案 1 :(得分:1)

答案 2 :(得分:1)

假设你有一个名为Person的模型..你想在视图中编辑一堆人并将它们发布到一个动作。

public ViewResult Edit()
{
    return View(list of persons to edit);
}

public ViewResult Edit(List<Person> persons)
{
    // save to db?
}

现在创建一个显示多人编辑的视图。

Edit.cshtml

@model List<Person>

@for (int i = 0; i < Model.Count; i++) {
   <h4>Person Number: @i</h4>
   @:First Name: @Html.EditorFor(m => m[i].FirstName)
   @:Last Name: @Html.EditorFor(m => m[i].LastName)
}