C# 如何在mvc的my controller中选中绑定到db的复选框
为什么需要将IsSeminar导入控制器的参数? 当您发送新闻对象时,模型绑定器将为您执行此操作 因此,我的建议是:C# 如何在mvc的my controller中选中绑定到db的复选框,c#,asp.net-mvc,C#,Asp.net Mvc,为什么需要将IsSeminar导入控制器的参数? 当您发送新闻对象时,模型绑定器将为您执行此操作 因此,我的建议是: public ActionResult Create(News news, HttpPostedFileBase NewsImageUrl,bool IsSeminar) { if (ModelState.IsValid) { string imagename = "no-photo.jpg";
public ActionResult Create(News news, HttpPostedFileBase NewsImageUrl,bool IsSeminar)
{
if (ModelState.IsValid)
{
string imagename = "no-photo.jpg";
if (NewsImageUrl != null)
{
imagename = Guid.NewGuid().ToString().Replace("-", "") +
Path.GetExtension(NewsImageUrl.FileName);
NewsImageUrl.SaveAs(Server.MapPath("/NewsImage/Images/" + imagename));
//------------------------Resize Image------------------------------
ImageResizer img = new ImageResizer();
img.Resize(Server.MapPath("/NewsImage/Images/" + imagename),
Server.MapPath("/NewsImage/Thumb/" + imagename));
}
news.NewsImageUrl = imagename;
news.CreateDate = DateTime.Now;
//-------------Seminar------------------------
if (IsSeminar)
{
news.IsSeminar = true;
db.Seminarparticipants.Add(new Seminarparticipants()
{
NewsID = news.NewsID,
FullName = String.Empty,
Phone = String.Empty,
Email = String.Empty
});
}
else
{
news.IsSeminar = false;
}
db.News.Add(news);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(news);
}
因为News对象具有同名的IsSeminar属性,绑定器将理解它并创建正确的对象。我不明白这里的问题是什么?另外,请不要只转储所有代码,请创建一个问题的列表。请编写正确的视图。我建议删除所有用Html编写的内容,因为它对问题没有用处。请只添加布尔代码。谢谢你的回复,samy sammour,我已经按照你说的做了,但它给了我这个错误(一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性)。内部错误是什么?这意味着它使用ModelState.IsValid失败。请尝试设置断点并使我们保持最新状态我已设置断点,并在这些字段上发现问题FullName=String.Empty,Phone=String.Empty,Email=String.Empty我删除了它们并解决了问题。非常感谢这太好了,为你感到高兴,你能为其他有同样问题的人做一个回答吗?我对这个网站不是很熟悉。
@using (Html.BeginForm("Create", "News", FormMethod.Post, new { enctype = "multipart/form-data" })) {
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.NewsTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NewsTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NewsTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NewsDescription, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NewsDescription, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NewsDescription, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NewsImageUrl, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Kendo().Upload().Name("NewsImageUrl").Multiple(false)
</div>
</div>
<div class="form-group">
<div class="col-md-2">
</div>
<div class="col-md-10">
@Html.LabelFor(model => model.IsSeminar)
@Html.EditorFor(model => model.IsSeminar)
@Html.ValidationMessageFor(model => model.IsSeminar)
</div>
</div>
public ActionResult Create(News news, HttpPostedFileBase NewsImageUrl,bool IsSeminar)
{
if (ModelState.IsValid)
{
string imagename = "no-photo.jpg";
if (NewsImageUrl != null)
{
imagename = Guid.NewGuid().ToString().Replace("-", "") +
Path.GetExtension(NewsImageUrl.FileName);
NewsImageUrl.SaveAs(Server.MapPath("/NewsImage/Images/" + imagename));
//------------------------Resize Image------------------------------
ImageResizer img = new ImageResizer();
img.Resize(Server.MapPath("/NewsImage/Images/" + imagename),
Server.MapPath("/NewsImage/Thumb/" + imagename));
}
news.NewsImageUrl = imagename;
news.CreateDate = DateTime.Now;
//-------------Seminar------------------------
if (IsSeminar)
{
news.IsSeminar = true;
db.Seminarparticipants.Add(new Seminarparticipants()
{
NewsID = news.NewsID,
FullName = String.Empty,
Phone = String.Empty,
Email = String.Empty
});
}
else
{
news.IsSeminar = false;
}
db.News.Add(news);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(news);
}
public ActionResult Create(News news, HttpPostedFileBase NewsImageUrl)
{
if (ModelState.IsValid)
{
if (news.IsSeminar)
{
}
}
}