2013-06-04 43 views
0

我想使用一個視圖,其中我顯示當前的結果,有能力添加一條新的記錄。我看了一下this的帖子,還有this的帖子,把我認爲應該可以工作的東西拼湊在一起,但它不會保存到數據庫中。這裏是我的視圖模型:使用一個視圖的索引和創建在mvc4

public class LabIndexViewModel 
    { 
     public Lab Lab { get; set; } 
     public IEnumerable<Lab> Labs { get; set; } 
    } 

而且在我的控制器我有這個在我的索引:

public ActionResult Index(int patid = 0, Lab lab = null) 
     { 
      ViewBag.Finalize = PatientSubmitted(patid); 
      ViewBag.DispPatientId = patid; 
      ViewBag.CheckButtonStatus = ButtonSubmitted(patid); 
      var labs = db.Labs.Where(l => l.PatientId == patid && l.Active); 
      LabIndexViewModel model = new LabIndexViewModel(); 
      model.Labs = labs.ToList(); 
      model.Lab = lab; 
      SetViewBagLists(); 
      return View(model); 
     } 
在我的崗位

然後在那裏將無法保存:

[HttpPost] 
     public ActionResult Create(LabIndexViewModel labindex) 
     { 
      ViewBag.DispPatientId = labindex.Lab.PatientId; 
      Lab lab = labindex.Lab; 

      try 
      { 
       lab.Active = true; 
       db.Labs.Add(lab); 
       db.SaveChanges(); 
       return RedirectToAction("Index", "Lab", new { patid = lab.PatientId }); 
      } 

      catch 
      { 
       ViewBag.Phase = new SelectList(StatusList(), "Text", "Value"); 
       ViewBag.Name = new SelectList(db.LabOptions, "Test", "Value", lab.Name); 
       return View(lab); 
      } 
     } 

這裏我的部分在哪裏我提交的數據在我看來:

@model PamperWeb.Models.LabIndexViewModel 

@using (Html.BeginForm("Create", "Lab")) { 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Lab</legend> 

     <tr> 
     <td> 
      @Html.DropDownList("Name", String.Empty) 
      @Html.ValidationMessageFor(model => model.Lab.Name) 
     </td> 

     <td> 
      @Html.EditorFor(model => model.Lab.Value) 
      @Html.ValidationMessageFor(model => model.Lab.Value) 
     </td> 

     <td> 
      @Html.EditorFor(model => model.Lab.Given) 
      @Html.ValidationMessageFor(model => model.Lab.Given) 
     </td> 

     <td> 
      @Html.EditorFor(model => model.Lab.TimeGiven) 
      @Html.ValidationMessageFor(model => model.Lab.TimeGiven) 
     </td> 

     <td> 
      @Html.DropDownList("Phase", String.Empty) 
      @Html.ValidationMessageFor(model => model.Lab.Phase) 
     </td> 

     @Html.HiddenFor(model => model.Lab.PatientId) 

     <td> 
      <input type="submit" value="Create" /> 
     </td> 
     </tr> 
    </fieldset> 
} 

任何人對如何做這項工作有什麼想法或有一個很好的例子?

+1

當你說它沒有保存時​​,你是什麼意思?你有沒有放置一個斷點來測試它是否進入'Create'動作? –

+0

@AndreCalil是的。它擊中控制器後,但一旦它擊中db.save(實驗室)它炸彈並擊中catch並重定向到創建get方法。 – Xaxum

+0

不錯!那麼例外是什麼......? –

回答

0

在評論的幫助下,我能夠找出問題所在。當我將參數添加到html.beginform時,它不再發送我的url參數與patientid。不知道爲什麼?我的正常創建視圖有這個,所以我的隱藏參數拿起了價值。我最終在我的控制器中設置了該值,以便表單中的隱藏參數能夠將其提取出來。這裏是我的GET指數是什麼,現在它解決了這個問題:

public ActionResult Index(int patid = 0, Lab lab = null) 
     { 
      ViewBag.Finalize = PatientSubmitted(patid); 
      ViewBag.DispPatientId = patid; 
      ViewBag.CheckButtonStatus = ButtonSubmitted(patid); 
      var labs = db.Labs.Where(l => l.PatientId == patid && l.Active); 
      LabIndexViewModel model = new LabIndexViewModel(); 
      if (lab == null) 
       lab = new Lab(); 
      lab.PatientId = patid; 
      model.Labs = labs.ToList(); 
      model.Lab = lab; 
      SetViewBagLists(); 
      return View(model); 
     } 

我也發現了這個post helpful在我發現我可以指定一個模型發送給部分。

1

我並不真的瞭解所有的問題,但我看到的東西錯了:

1)此致PartialView必須張貼一個實驗室,因此,使它強類型的實驗室,因爲HTML助手將生成HTML的默認的模型綁定器無法處理使用LabIndexViewModel構建模型早在服務器:

@model PamperWeb.Models.Lab 

@using (Html.BeginForm("Create", "Lab")) { 
    @Html.ValidationSummary(true) 
    <fieldset> 
    <legend>Lab</legend> 
    <tr> 
     <td> 
     @Html.DropDownList("Name", String.Empty) 
     @Html.ValidationMessageFor(model => model.Name) 
     </td> 

    <td> 
     @Html.EditorFor(model => model.Value) 
     @Html.ValidationMessageFor(model => model.Value) 
    </td> 

    <td> 
     @Html.EditorFor(model => model.Given) 
     @Html.ValidationMessageFor(model => model.Given) 
    </td> 
    <td> 
     @Html.EditorFor(model => model.TimeGiven) 
     @Html.ValidationMessageFor(model => model.TimeGiven) 
    </td> 

    <td> 
     @Html.DropDownList("Phase", String.Empty) 
     @Html.ValidationMessageFor(model => model.Phase) 
    </td> 
     @Html.HiddenFor(model => model.PatientId) 
    <td> 
     <input type="submit" value="Create" /> 
    </td> 
    </tr> 
</fieldset> 
} 

2)改變控制器操作創建接收爲參數貼實驗室:

[HttpPost] 
public ActionResult Create(Lab lab) 
{ 
    ViewBag.DispPatientId = Lab.PatientId; 

    try 
    { 
    lab.Active = true; 
    db.Labs.Add(lab); 
    db.SaveChanges(); 
    return RedirectToAction("Index", "Lab", new { patid = lab.PatientId }); 
    } 
    catch 
    { 
    ViewBag.Phase = new SelectList(StatusList(), "Text", "Value"); 
    ViewBag.Name = new SelectList(db.LabOptions, "Test", "Value", lab.Name); 
    return View(lab); 
    } 
} 

3)使用創建的ViewModel來顯示實驗室!這就是ViewModel的主要目的,在視圖中顯示覆雜類型!任何其他操作都需要創建一個自定義ModelBinder來貫穿請求,並將模型構建回服務器。

希望這對你有所幫助!我真的從這個問題得到了這個!

+0

感謝您的幫助。我最終使用這一部分來讓事情變得更簡單。 – Xaxum

+0

沒問題!希望這可以幫助你!如果這解決了這個問題,不要忘記成爲一個問題! – Fals

+1

實際的答案是@AndreCalil在上面的評論中提出的。我錯過了必填字段,因爲我添加了參數。查看我的發佈結果的更多解釋。 – Xaxum