【发布时间】:2017-05-06 16:43:10
【问题描述】:
我正在尝试填充列表框并将所选选项解析回控制器。问题是我似乎无法弄清楚 BeginForm 和 Listboxfor。网上找到的教程只会让我更加困惑。
这是我的csHtml:
@Html.BeginForm("BtnSelectPost", "TimelinePageController")
{
<div>
<select id="s1" class="timeline" size="20" name="Options">
@foreach (var C in Model)
{
Html.ListBoxFor(?)
}
</select>
</div>
}
这是我的控制器:
public List<Contribution> Contriblist = new List<Contribution>();
// GET: TimelinePage
public ActionResult TimelinePage(Event Event)
{
Contriblist = DatabaseGetContribution.GetContributions(Event.ID);
return View(Contriblist);
}
public SelectList GetAllContrib()
{
SelectList Contribslist = new SelectList(Contriblist, "ID", "Text");
return Contribslist;
}
我需要做什么才能让它工作?
任何帮助都会很棒
编辑: 我很抱歉缺乏信息: 我正在使用 2 个模型。 贡献:
public int ID { get; set; }
public int ContributionID { get; set; }
public DateTime DateTime { get; set; }
public string Category { get; set; }
public int Likes { get; set; }
public int Reports { get; set; }
public int PostID { get; set; }
public byte[] File { get; set; }
public string Attachment { get; set; }
public Message Message { get; set; }
/// <summary>
/// Create Constructor
/// </summary>
/// <param name="category">The category of a post</param>
/// <param name="likes">The amount of likes of a post</param>
/// <param name="file">The file belonging to the post</param>
/// <param name="postID">This is the ID of the post reacted to</param>
public Contribution(DateTime datetime, string category, int likes, int reports, int postid, Message message)
{
this.Category = category;
this.Likes = likes;
this.Reports = reports;
this.PostID = postid;
this.DateTime = datetime;
this.Message = message;
}
和 ViewModelContrib:
public IEnumerable<SelectListItem> contrib { get; set; }
contribution 中的值取自数据库,Event 仅用于获取 ID 以检查应从数据库中获取哪些贡献。
感谢大家的帮助。
【问题讨论】:
-
你的型号是什么?您绑定到什么属性?您在哪里将
SelectList返回到视图中? (并从 GET 方法中删除您的Event Event参数) -
您的模型甚至不包含列表框可以绑定到的属性
-
Html.DropDownListFor(
, new SelectList( , "", ""))
标签: c# asp.net-mvc razor html.beginform html.listboxfor