[英]Can't bind dropdownlist in a partial view in Code First Entity Framework
我是MVC的新手,所以對菜鳥的問題深表歉意!
我有以下模型(TimeEntry),用於輸入時間表詳細信息:
namespace MVCLogin.Models
{
public class TimeEntry
{
public int TimeEntryID { get; set; }
public int TaskTypeID { get; set; }
[ForeignKey("TaskTypeID")]
public virtual TaskType TaskType { get; set; }
public double MonHours { get; set; }
public double TueHours { get; set; }
public double WedHours { get; set; }
public double ThuHours { get; set; }
public double FriHours { get; set; }
}
}
任務類型基於以下模型:
namespace MVCLogin.Models
{
public class TaskType
{
public int TaskTypeID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
我使用以下局部視圖來輸入時間表詳細信息:
<div class="col-md-1">
@Html.EditorFor(model => model.TaskType)
@Html.ValidationMessageFor(model => model.TaskType)
</div>
<div class="col-sm-1">
@Html.TextBoxFor(model => model.MonHours , new { style = "width:100%", @class = "hours mon" })
@Html.ValidationMessageFor(model => model.MonHours)
</div>
<div class="col-sm-1">
@Html.TextBoxFor(model => model.TueHours, new { style = "width:100%", @class = "hours tue" })
@Html.ValidationMessageFor(model => model.TueHours)
</div>
<div class="col-sm-1">
@Html.TextBoxFor(model => model.WedHours, new { style = "width:100%", @class = "hours wed" })
@Html.ValidationMessageFor(model => model.WedHours)
</div>
<div class="col-sm-1">
@Html.TextBoxFor(model => model.ThuHours, new { style = "width:100%", @class = "hours thu" })
@Html.ValidationMessageFor(model => model.ThuHours)
</div>
<div class="col-sm-1">
@Html.TextBoxFor(model => model.FriHours, new { style = "width:100%", @class = "hours fri" })
@Html.ValidationMessageFor(model => model.FriHours)
</div>
我希望任務類型字段為下拉列表,但我不知道如何將其連接起來。 這些類和數據是正確的,就像我為TimeEntry類搭建一個控制器(使用Visual Studio的“添加控制器”工具)一樣,它可以正常工作:
<div class="form-group">
@Html.LabelFor(model => model.TaskTypeID, "TaskTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("TaskTypeID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TaskTypeID, "", new { @class = "text-danger" })
</div>
</div>
如何使下拉列表在局部視圖中工作?
Html.DropDownList
的null參數是Dropdown的數據,並且是Ienumerable<SelectList>
。
為了傳遞數據。 您需要將TaskType
列表轉換為SelectListItem
列表(您可以在Controller或其他地方執行此操作,然后再將Model從Controller發送到View),然后將其放入模型中
namespace MVCLogin.Models
{
public class TimeEntry
{
public int TimeEntryID { get; set; }
public int TaskTypeID { get; set; }
[ForeignKey("TaskTypeID")]
public virtual TaskType TaskType { get; set; }
public IEnumerable<SelectListItem> TaskTypeSelectListItems { get; set; }
public double MonHours { get; set; }
public double TueHours { get; set; }
public double WedHours { get; set; }
public double ThuHours { get; set; }
public double FriHours { get; set; }
}
}
下拉代碼將更改為
<div class="form-group">
@Html.LabelFor(model => model.TaskTypeID, "TaskTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("TaskTypeID", model => model.TaskTypeSelectListItems, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TaskTypeID, "", new { @class = "text-danger" })
</div>
</div>
請注意,當您發布/獲取到服務器時,所選值將綁定到模型上的“ TaskTypeID”。
根據您的請求:ViewBag是一個屬性,使您可以將控制器中的值動態共享到視圖。 由於它是動態的,因此請記住,您將需要顯式轉換對象,以便剃刀引擎知道它們在運行時的狀態。 請注意,在閱讀有關內容(底部的更多內容)時,ViewBag壽命很短,僅存在於當前請求中,因此,如果控制器中出現模型狀態錯誤,則需要將數據重新加載到視圖包中。
這將有助於保持模型/視圖模型的整潔。
接下來是代碼,測試設置:我有我的主頁(索引),並且有一個局部視圖(AddSomething)被加載到我的主索引頁面中。 給定您的模型,這就是我的控制器的外觀。 請注意,我最初是在父頁面中加載視圖包,但是如果提交時發生錯誤,則由於動態對象存在時間太短,我需要重新加載視圖包。
DALHelper dalHelp = new DALHelper();
public ActionResult Index()
{
//Get a list of task types from the data access layer
//Cast them into a SelectItemList and pass them into viewBag
ViewBag.TaskDD = dalHelp.GetTaskTypes().Select(a => new SelectListItem { Text = a.Name, Value = a.TaskTypeID.ToString() }).ToList();
return View();
}
public ActionResult AddSomething()
{
TimeEntry te = new TimeEntry();
return View(te);
}
[HttpPost]
public ActionResult AddSomething(TimeEntry te)
{
if (ModelState.IsValid)
{
//call my Data Access Layer to add new entry and redirect to wherver on success
if (dalHelp.CreateTimeEntry(te))
{
return RedirectToAction("Index");
}
else
{
//something happened during adding entry into DB, write whatever code to handle it gracefully
//I need to reload my viewbag since it has since been cleared (short lived)
ViewBag.TaskDD = dalHelp.GetTaskTypes().Select(a => new SelectListItem { Text = a.Name, Value = a.TaskTypeID.ToString() }).ToList();
ModelState.AddModelError("TimeEntryID", "An Error was encountered...");
return View(te);
}
}
else
{
//I need to reload my viewbag since it has since been cleared (short lived)
ViewBag.TaskDD = dalHelp.GetTaskTypes().Select(a => new SelectListItem { Text = a.Name, Value = a.TaskTypeID.ToString() }).ToList();
return View(te);
}
}
在我的局部視圖中,我需要轉換我的viewbag項,因為它是動態類型,因此剃刀引擎可以處理:另外,我不是在使用Dropdownlist而不是dropdownlist:
@model deletemeweb2.Models.TimeEntry
@using (Html.BeginForm("AddSomething", "Home"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>TimeEntry</h4>
@Html.ValidationMessageFor(model => model.TimeEntryID, "", new { @class = "text-danger" })
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.TaskTypeID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.TaskTypeID, (IEnumerable<SelectListItem>)ViewBag.TaskDD, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TaskTypeID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MonHours, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MonHours, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MonHours, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TueHours, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TueHours, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TueHours, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.WedHours, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.WedHours, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.WedHours, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ThuHours, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ThuHours, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ThuHours, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FriHours, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FriHours, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FriHours, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
有關處理不同控制器需求時有關ViewBag和類似屬性的更多信息,請訪問: http ://www.c-sharpcorner.com/blogs/viewdata-vs-viewbag-vs-tempdata-in-mvc1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.