ASP将数据从视图(两个模型)发布到控制器

时间:2013-11-26 20:21:29

标签: asp.net-mvc post view controller viewmodel

我正在尝试使用post从视图发送数据。我在一个viewModel中使用两个模型,但在控制器中不接受数据。

这是我的 ViewModel (我的模型是通过实体框架从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 中得到null。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

好的,最后我发现了“正确的”(工作)解决方案。我试图弄清楚一个新项目。它现在对我有用。我希望这有助于某人。

这里的问题出现在我的创建视图中

我必须使用强类型助手(DropDownListFor(model =&gt; model。...),如下所示:

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

而不是:

@Html.DropDownList("ID_DIAGNOZY", string.Empty)