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中得到空值。
謝謝你的時間。
無法找出一個出來,似乎對我很好。:( – hutchonoid
沒關係,仍然感謝您的嘗試 – suchoss
沒有probs,祝你好運 – hutchonoid