public partial class News {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public News() {
this.Seminarparticipants = new HashSet<Seminarparticipants>();
}
public int NewsID { get; set; }
public string NewsTitle { get; set; }
public string NewsDescription { get; set; }
public string NewsImageUrl { get; set; }
public System.DateTime CreateDate { get; set; }
public bool IsSeminar { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Seminarparticipants> Seminarparticipants { get; set; }
}
}
这是我的观点
@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);
}
答案 0 :(得分:0)
为什么需要将IsSeminar导入控制器的参数? 当您发送新闻对象时,模型绑定器将为您执行此操作。
所以我的建议是:
public ActionResult Create(News news, HttpPostedFileBase NewsImageUrl)
{
if (ModelState.IsValid)
{
if (news.IsSeminar)
{
}
}
}
因为News对象具有相同名称的IsSeminar属性,所以绑定器将理解它并创建正确的对象。