下拉列表错误:&QUOT;有型的没有ViewData的项目'的IEnumerable&LT; SelectListItem&GT;' [英] DropDown List error : "There is no ViewData item of type 'IEnumerable<SelectListItem>'
问题描述
试图改变我的以PartialView
,我用它与标签系统,我得到这个错误:有型无ViewData的项目IEnumerable的&LT; SelectListItem&GT;'有在
: _CreateCit.cshtml
文件的第30行的关键REGION_ID
Trying to change my view to PartialView
, that I use it with tabs System, I got this error : There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'REGION_ID'.
at line 30 of _CreateCit.cshtml
file :
@model pfebs0.Models.CITOYEN
<h2>Demandeur Info</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
// some code removed (HTML tag)
<div class="form-group">
<label for="REGION_ID" >Region</label>
@Html.DropDownList("REGION_ID", String.Empty)
@Html.ValidationMessageFor(model => model.REGION_ID)
</div>
// some other input tag
<br />
<button type="submit" class="btn btn-default">Valider</button>
<button type="reset" class="btn btn-default">Annuler</button>
</div>
}
和本我的 index.cshtml
file`:
And this my index.cshtml
file`:
@{
ViewBag.Title = "Creation D'un nouveau Demande";
}
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a href="#tab1">Add user tab</a></li>
<li><a href="#tab2">add request tab</a></li>
</ul>
<div class="tab-content">
<div id="tab1" class="tab-pane active">
@Html.Partial("_CreateCit", ViewData)
</div>
<div id="tab2" class="tab-pane">
@Html.Partial("_CreateDM")
</div>
</div>
@section Scripts {
// script Section removed
}
她是我的控制器code:
her's my Controller code :
public ActionResult CreateCit()
{
ViewData["REGION_ID"] = new SelectList(db.REGION, "REGION_ID", "NOM");
return PartialView("_CreateCit");
}
//
// POST: /Citoyen/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateCit(CITOYEN citoyen)
{
if (ModelState.IsValid)
{
db.CITOYEN.Add(citoyen);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.REGION_ID = new SelectList(db.REGION, "REGION_ID", "NOM", citoyen.REGION_ID);
return PartialView(citoyen);
}
标准视图中的相同code的工作,我只是改变了返回查看
到返回PartialView(VIEWNAME)
我编辑的CSHTML文件。
我想我错过了一些东西!
The same code work with standard view, I just changed return View
to return PartialView(Viewname)
and I edited the cshtml file.
I think I missed something !!
推荐答案
的 @ Html.Partial
只是呈现局部视图,并且不调用控制器动作。它只是需要局部视图code,没有达到控制器(设定在动作一个破发点,并测试这个)。在的ViewData code>将不包含你认为它会的数据。您需要使用
@ Html.Action
:
The @Html.Partial
just renders the partial view and does not call the controller action. It just takes the partial view code, without reaching the controller (set a break point in the action and test this). The ViewData
will not contain the data that you think it will. You need to use @Html.Action
:
<div id="tab1" class="tab-pane active">
@Html.Action("CreateCit")
</div>
<div id="tab2" class="tab-pane">
@Html.Action("CreateDM")
</div>
另外,尽量明确将在的ViewData code>对象中的数据,
@ Html.Partial
之前调用:
Alternatively, try explicitly adding the data in the ViewData
object, before calling @Html.Partial
:
<div id="tab1" class="tab-pane active">
@{
ViewData["REGION_ID_DDL"] = ViewData["REGION_ID"]
}
@Html.Partial("_CreateCit", ViewData)
</div>
编辑:
为了满足您对成功消息的评论:有几件事情可以做,以显示成功消息。我个人最喜欢的是创建一个 ResultModel
:
public class ResultModel
{
public string Message { get; set; }
public ResultType TypeOfResult { get; set; }
}
public Enum TypeOfResult
{
Error,
Success
}
创建,它接受一个局部视图 ResultModel
并显示以下信息:
@model YourNamespace.Models.ResultModel
<p>Sort du resultat: @Model.TypeOfResult</p>
<p>Message: @Model.Message</p>
然后,您可以叫它是这样的:
You would then just call it like this:
if (ModelState.IsValid)
{
db.CITOYEN.Add(citoyen);
db.SaveChanges();
ResultModel resultModel = new ResultModel();
resultModel.TypeOfResult = TypeOfResult.Success;
//my french is a bit rusty but the result from Google Translate sounds good enough
resultModel.Message = "Citoyen ajouté avec succès.";
return PartialView("_ResultSummary", resultModel);
}
当然,这可以被用来描绘出错误为好。你可以添加一个的try-catch
围绕 db.SaveChanges();
通话,然后设置成功类型/消息在尝试
块和错误类型/消息中的抓
块。
Of course, this can be used to trace out errors as well. You could add a try-catch
around the db.SaveChanges();
call and set the success type/message in the try
block and the error type/message in the catch
block.
这种方法的另一个好处是, ResultModel
对象也可以作为一个JSON字符串的情况下,要显示一个JavaScript警报,甚至自定义发送的,弹出。
Another advantage of this method is that the ResultModel
object can also be sent as a JSON string, in case you want to display a JavaScript "alert" or even a custom pop-up.
这篇关于下拉列表错误:&QUOT;有型的没有ViewData的项目'的IEnumerable&LT; SelectListItem&GT;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!