2013-11-26 58 views
1

我試圖使用帖子從視圖發送數據。我在一個viewModel中使用兩個模型,但在控制器中不接受數據。ASP將數據從視圖(兩個模型)發送到控制器

這是我的視圖模型(從DB由實體框架生成的我的模型):

using System.Collections.Generic; 
using RychleSkody.Models; 

namespace RychleSkody.Models.ViewModels 
{ 
    public class LikvidaceDiagnoza 
    { 
     public LIKVIDACE LIKVIDACE { get; set; } 
     public LIKVIDACE_DIAGNOZY LIKVIDACE_DIAGNOZY { get; set; } 
    } 
} 

這是我的控制器

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(LikvidaceDiagnoza likvidace) 
    { 
     if (ModelState.IsValid) 
     { 
      db.LIKVIDACE.Add(likvidace.LIKVIDACE); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     ViewBag.ID_FREKVENCE = new SelectList(db.FREKVENCE, "ID_FREKVENCE", "NAZEV", likvidace.LIKVIDACE.ID_FREKVENCE); 
     ViewBag.ID_SAZBY = new SelectList(db.SAZBA, "ID_SAZBY", "NAZEV", likvidace.LIKVIDACE.ID_SAZBY); 
     ViewBag.ID_PRODUKTU = new SelectList(db.PRODUKT, "ID_PRODUKTU", "NAZEV", likvidace.LIKVIDACE.ID_PRODUKTU); 
     ViewBag.ID_LIKVIDACE_STAV = new SelectList(db.LIKVIDACE_STAV, "ID_LIKVIDACE_STAV", "NAZEV", likvidace.LIKVIDACE.ID_LIKVIDACE_STAV); 
     ViewBag.ID_TN_LIMITU = new SelectList(db.TN_LIMIT, "ID_TN_LIMITU", "ID_TN_LIMITU", likvidace.LIKVIDACE.ID_TN_LIMITU); 
     ViewBag.ID_TN_POSOUZENI = new SelectList(db.TN_POSOUZENI, "ID_TN_POSOUZENI", "NAZEV", likvidace.LIKVIDACE.ID_TN_POSOUZENI); 
     ViewBag.ID_UZIVATELE = new SelectList(db.UZIVATEL, "ID_UZIVATELE", "JMENO", likvidace.LIKVIDACE.ID_UZIVATELE); 
     //ViewBag.ID_DIAGNOZY = new SelectList(db.DIAGNOZA, "ID_DIAGNOZY", "NAZEV"); //opravit VIEW a přepsat do podobného stylu 
     ViewBag.VybraneDiagnozy = seznamVybranychDiagnoz; 
     ViewBag.New = false; 
     return View(likvidace); 

    } 

,這是我視圖

@model RychleSkody.Models.ViewModels.LikvidaceDiagnoza 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>LIKVIDACE</legend> 
     <div class="editor-label"> 
      DIAGNOZA 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownListFor(x => x.LIKVIDACE_DIAGNOZY.ID_DIAGNOZY, ViewBag.ID_DIAGNOZY as SelectList, string.Empty) 
      @Html.DropDownListFor(x => x.LIKVIDACE_DIAGNOZY, ViewBag.ID_STRANY as SelectList, string.Empty) 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("ID_DIAGNOZY", ViewBag.ID_DIAGNOZY as SelectList, string.Empty) 
      @Html.DropDownList("ID_STRANY", string.Empty) 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("ID_DIAGNOZY", string.Empty) 
      @Html.DropDownList("ID_STRANY", string.Empty) 
     </div> 
... 

正如您從視圖中看到的,我試圖找到正確的解決方案,但迄今爲止我還沒有取得成功。每當我在控制器的輸入變量likvidace中得到空值。

謝謝你的時間。

+0

無法找出一個出來,似乎對我很好。:( – hutchonoid

+0

沒關係,仍然感謝您的嘗試 – suchoss

+0

沒有probs,祝你好運 – hutchonoid

回答

0

好吧終於我找到了「正確」(工作)的解決方案。我試圖找出一個新項目。它現在正在爲我工​​作。我希望這可以幫助別人。

這裏的問題是在我創建視圖

我必須使用強類型的輔助(DropDownListFor(型號=>模型...)這樣的:

@Html.DropDownListFor(model => model.LIKVIDACE.ID_UZIVATELE, ViewBag.ID_UZIVATELE as SelectList, String.Empty) 

代替:

@Html.DropDownList("ID_DIAGNOZY", string.Empty) 
相關問題