【发布时间】:2016-07-21 08:58:24
【问题描述】:
如果出现异常,我正在尝试返回模型以查看以保留用户输入的数据。所以当我返回模型查看它告诉我“值不能为空或为空”但模型仍然有数据
@Html.EditorFor(model => model.NewsTitle, new { htmlAttributes = new { @class = "form-control" } })
[HttpGet]
public ActionResult Edit(int? Id)
{
using (var db = new NewsDatabaseEntities())
{
db.Configuration.LazyLoadingEnabled = false;
var Result = (from n in db.News.Include("Categories")
from c in db.Categories
where n.NewsId == Id
select new { news = n, neweCategories = n.Categories, cate = c }).ToList();
News NewsDetails = (from news in Result
select new News
{
NewsId = news.news.NewsId,
NewsTitle = news.news.NewsTitle,
NewsBody = news.news.NewsBody,
NewsImagePath = news.news.NewsImagePath,
Categories = news.neweCategories
}).FirstOrDefault();
var AllCategories = (from c in Result
select new Category
{
CategoryId = c.cate.CategoryId,
CategoryName = c.cate.CategoryName
}).ToList();
if (NewsDetails != null)
{
var model = new NewsViewModel();
model.NewsId = NewsDetails.NewsId;
model.AllCategories = AllCategories;
model.Categories = NewsDetails.Categories;
model.NewsTitle = NewsDetails.NewsTitle;
model.NewsBody = NewsDetails.NewsBody;
model.NewsImagePath = NewsDetails.NewsImagePath;
return View(model);
}
else
{
return RedirectToAction("Index", "Home");
}
}
return View();
}
[ValidateInput(false)]
[ValidateAntiForgeryToken]
public ActionResult Edit(NewsViewModel model)
{
using (var db = new NewsDatabaseEntities())
{
db.Configuration.LazyLoadingEnabled = false;
News NewsToUpdate = db.News.Include("Categories").SingleOrDefault(n => n.NewsId == model.NewsId);
if (NewsToUpdate != null)
{
using (var transaction = db.Database.BeginTransaction())
{
try
{
db.SaveChanges();
transaction.Commit();
return RedirectToAction("Details", "Admin", new { Id = model.NewsId });
}
catch (Exception ex)
{
transaction.Rollback();
TempData["Error"] = ex.Message + " " + ex.ToString();
return View(model);
}
}
}
else
{
return RedirectToAction("Index", "Home");
}
}
return View();
}
剃刀视图
@model Assignment.Models.NewsViewModel
@{
ViewBag.Title = "Edit";
}
<h2>Update News</h2>
<br />
@if (TempData["Error"] != null)
{
<p class=" alert alert-danger">
<strong>Error! </strong>
@TempData["Error"]
</p>
}
@using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { id = "MyForm", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.NewsId)
@Html.HiddenFor(model => model.SelectedCategoriesIds, new { id = "hid" })
<div class="form-group">
@Html.Label("Enter News Title", 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" })
<br /><br />
<img src="@Url.Content(Model.NewsImagePath)" alt="Image" id="ImagePreview" width=330 height=250 />
<br /><br />
@Html.TextBoxFor(model => model.NewsImageFile, new { type = "file", data_val = "false" })
@Html.ValidationMessageFor(model => model.NewsImageFile, "", new { @class = "text-danger" })
<br /> <br />
<div class="form-group">
<div class="col-md-10">
@Html.TextAreaFor(model => model.NewsBody, new { htmlAttributes = new { @class = "ckeditor" } })
@Html.ValidationMessageFor(model => model.NewsBody, "", new { @class = "text-danger" })
<br />
This News under these categories: <br /><br />
<select id="dropdownOne" name="dropdownOne" multiple="multiple" class="form-control">
@foreach (var Category in Model.AllCategories)
{
if (Model.Categories.Select(c => c.CategoryId).Contains(Category.CategoryId))
{
<option selected value="@Category.CategoryId">@Category.CategoryName</option>
}
else
{
<option value="@Category.CategoryId">@Category.CategoryName</option>
}
}
</select>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
【问题讨论】:
-
您认为哪一行出现错误?
-
@Shyju,谢谢你的帮助。这一行 Html.EditorFor(model => model.NewsTitle, new { htmlAttributes = new { class= "form-control" } })
-
这通常是在发布后返回视图时未填充模型的所有属性的情况。在从 httppost 返回模型之前,您需要确保在初始 httpget 操作期间填充在模型上的任何属性也得到填充。以及您的视图需要的任何 ViewBag 或 TempData 对象也被设置
-
@JamieD77 非常感谢
标签: asp.net-mvc razor