我想在表单上填充创建视图的下拉列表。这是我的代码
@model CM.Models.TVSerialEpisode
@{
ViewBag.Title = "AddEpisode";
}
@Html.Action("ListAllSeason","TvSerial") //here i want to call this action method so i could bind all tv serial
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>TVSerialEpisode</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Serial_ID)
</div>
<div class="editor-field">
@Html.DropDownList("Serial_ID", new List<SelectListItem>
{
new SelectListItem { Text = "Backstrom", Value="16" },
new SelectListItem{Text="10 Things I hate about You", Value="15"},
new SelectListItem { Text = "Castle", Value="14" }
}, "Select Serial")
@Html.ValidationMessageFor(model => model.Serial_ID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Episode_No)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Episode_No)
@Html.ValidationMessageFor(model => model.Episode_No)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Episode_Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Episode_Name)
@Html.ValidationMessageFor(model => model.Episode_Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.File_Url_480p)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.File_Url_480p)
@Html.ValidationMessageFor(model => model.File_Url_480p)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.File_Url_720p)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.File_Url_720p)
@Html.ValidationMessageFor(model => model.File_Url_720p)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Is_Active)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Is_Active)
@Html.ValidationMessageFor(model => model.Is_Active)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
这是模型类
namespace CM.Models
{
[Table("tbltvserial")]
public class TVSerial
{
[Key]
public Int64 Serial_ID { get; set; }
public string Serial_Name { get; set; }
public string Season_Name { get; set; }
public int Season_No { get; set; }
public int? Release_Year { get; set; }
}
public class TVSerial_VM
{
public Int64 Serial_ID { get; set; }
public string Serial_Name { get; set; }
public int Season_No { get; set; }
}
[Table("tblserialepisode")]
public class TVSerialEpisode
{
[Key]
public Int64 Video_ID { get; set; }
public Int64 Serial_ID { get; set; }
public int Episode_No { get; set; }
public string Episode_Name { get; set; }
public string File_Url_480p { get; set; }
public string File_Url_720p { get; set; }
public string Description { get; set; }
public bool Is_Active { get; set; }
public DateTime Uploaded_Time { get; set; }
}
public class TvSerialContext : DbContext
{
public DbSet<TVSerialEpisode> TvSerialEpisodes { get; set; }
public DbSet<TVSerial> TvSerials { get; set; }
}
}
这是控制器类
public class TvSerialController : Controller
{
public List<TVSerial_VM> ListAllSeason()
{
try
{
TvSerialContext tvContext = new TvSerialContext();
List<TVSerial_VM> tv = tvContext.TvSerials
.Select(t => new TVSerial_VM
{
Serial_Name = t.Serial_Name,
Season_No=t.Season_No,
Serial_ID=t.Serial_ID
}).OrderBy(t=> t.Season_Name).
ToList();
return (tv);
}
catch (Exception ex)
{
return null;
}
}
}
public class TvSerialEpisodeController : Controller
{
TvSerialContext tvContext = new TvSerialContext();
public ActionResult AddEpisode()
{
return View();
}
[HttpPost]
public ActionResult AddEpisode(TVSerialEpisode tvEpisode)
{
if (ModelState.IsValid)
{
tvContext.TvSerialEpisodes.Add(tvEpisode);
tvContext.SaveChanges();
return RedirectToAction("AddEpisode");
}
return View(tvEpisode);
}
}
我不知道如何在上面的视图页面中绑定tvserial列表。基本上我想从下拉列表中选择序列ID,这是否可以在创建视图中调用一个动作方法。
答案 0 :(得分:1)
要从数据库填充SelectList
,请在控制器AddEpisode()
方法中使用
ViewBag.SerialList = db.TvSerials.ToList().Select(x => new SelectListItem(){ Value = x.Serial_ID.ToString(), Text = x.Serial_Name });
然后在视图中使用
@Html.DropDownListFor(m => m.Serial_ID, (IEnumerable<SelectListItem>)ViewBag.SerialList, "Select Serial")
虽然建议您使用视图模型并在视图模型中包含IEnumerable<SelectListItem> SerialList
属性,以便
@Html.DropDownListFor(m => m.Serial_ID, Model.SerialList, "Select Serial")