2012-05-29 74 views
3

是否有可能有一個編輯多個記錄的視圖,就像index.cshtml視圖通過記錄循環顯示它們一樣(如下所示)?asp.net mvc更新多條記錄

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

因此,對於上面的每一行,它將涉及到數據庫中的不同行。

有誰知道任何示例顯示如何實現?

感謝任何指針,

馬克

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

}

控制器:

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

我堅持控制器,如果有人可以提供任何幫助?

再次感謝,

馬克

月2日更新

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

回答

7

看看使用的編輯模板

假設你的視圖模型/型號看起來像這樣

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通貨膨脹方法

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

編輯:基於用戶的評論和更新問題。

請求到OP:當你張貼問題,包括所有相關細節,以在第一時間本身問題下一次)

創建一個視圖模型來包裝你的列表目標類。

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

而在你的GET操作方法發送該包裝查看模型觀與充滿

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斷點來查看要發佈的值。

+0

嗨 - 我與控制器掙扎 - 你將能夠幫助嗎?我的模型是:public class objectives { public int ID {get;組; } public int tvid {get;組; } public string tlnt {get;組; } public DateTime month {get;組; } public string objective {get;組; } public int score {get;組; } public int possscore {get;組; } public string comments {get;組; }} 我 – Mark

+0

控制器,其中我卡是:[HttpPost] 公共的ActionResult編輯(目標的目標) { 如果(ModelState.IsValid) { db.Entry(目標)= .STATE EntityState.Modified; foreach(objective objective in objectives.objective) {} db.SaveChanges(); return RedirectToAction(「Index」); } return View(objectives); } – Mark

+0

這裏有多個項目在這裏? – Shyju

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