我正在尝试填充列表框并将所选选项解析回控制器。问题是我似乎无法弄清楚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; }
贡献中的值来自数据库,而事件仅用于获取ID,以检查应从数据库中获取哪些贡献。
谢谢大家的帮助。
答案 0 :(得分:0)
仅用于将数据绑定到listview
并提交表单
首先创建一个Model类和DbContext
public class Country
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
public class ConuntryContext : DbContext
{
public virtual DbSet<Country> Country { get; set; }
}
下一步创建控制器
public class TestController : Controller
{
ConuntryContext db = new ConuntryContext();
public ActionResult Index()
{
db.Country.Add(new Country {
CountryId = 1,
CountryName = "India"
});
db.Country.Add(new Country
{
CountryId = 2,
CountryName = "USA"
});
db.Country.Add(new Country
{
CountryId = 3,
CountryName = "UK"
});
db.SaveChanges();
var countries = db.Country.ToList();
ViewBag.countries = new SelectList(countries, "CountryId", "CountryName");
return View();
}
[HttpPost]
public ActionResult Action(Country country)
{
var countries = db.Country.ToList();
ViewBag.countries = new SelectList(countries, "CountryId", "CountryName");
ViewBag.selectedValue = db.Country.Where(a => a.CountryId == country.CountryId).Select(a => a.CountryName).SingleOrDefault();
return View();
}
}
最后创建视图
@using (Html.BeginForm("Index", "Test", FormMethod.Post))
{
@Html.ListBox("CountryId", (SelectList)ViewBag.countries)
<br/>
<button type="submit">Submit</button>
}
<p>You have selected @ViewBag.selectedValue Country</p>
答案 1 :(得分:-1)
这是一个解决方案 将属性添加到您的类或ViewModel
public IEnumerable<SelectListItem> YourPropertyName{get;set;}
然后在你的控制器中绑定列表框的值 然后在你的视图中
@Html.BeginForm("BtnSelectPost", "TimelinePageController")
{
<div>
@Html.ListBoxFor(m=>m.ModelPropertyYouWantToBindTo,Model.YourPropertyName)
</div>
}
那就是它。列表框现在应该显示值