У меня есть представление под названием ShowProduct. Внутри этого представления у меня есть частичное представление под названием CreateComment. Когда я вызываю частичное представление CreateComment через браузер, информация внутри этого частичного представления легко сохраняется в базе данных SQL, и ModelState является допустимым. Но когда я вызываю представление ShowProduct через браузер, информация внутри частичного представления CreateComment не сохраняется в базе данных, а информация внутри того же частичного представления указывается в адресной строке браузера
public ActionResult CreateComment(int id)
{
return PartialView(new ProductComment() { ProductID = id });
}
[HttpPost]
public ActionResult CreateComment(ProductComment PC)
{
if (ModelState.IsValid)
{
PC.CreateDate = DateTime.Now;
db.ProductComments.Add(PC);
db.SaveChanges();
return RedirectToAction("ShowProduct", new { id = PC.ProductID });
}
return PartialView(PC);
}
ProductComment Класс:
#region Property
[Key]
public int CommentID { get; set; }
[Required]
[TypeConverter("Nvarchar")]
[MaxLength(150)]
public string Name { get; set; }
[Required]
[TypeConverter("NVarchar")]
[MaxLength(350)]
[EmailAddress]
public string Email { get; set; }
[TypeConverter("NVarchar")]
[MaxLength(350)]
public string WebSite { get; set; }
[Required]
[MaxLength(800)]
public string Comment { get; set; }
public DateTime CreateDate { get; set; }
#endregion
//*******************************************
#region Relation
public virtual int ProductID { get; set; }
public virtual Product Product { get; set; }
#endregion
CreateComment.cs html Код:
@model OnlineShop_DataLayer.Models.ProductComment
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.ProductID)
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2})
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.WebSite, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.WebSite, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.WebSite, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Insert Comment" class="btn btn-primary" />
</div>
</div>
</div>
}