Khi xác thực không thành công, tôi nên trở lại trang nào? Lượt xem(); hoặc Xem (mô hình); ?Nếu (ModelState.IsValid == false) trả về View(); hoặc Xem (mô hình) ;?
Tôi nhận thấy cả hai công việc. Nó là khó hiểu.
EDIT:
public class MoviesController : Controller
{
MoviesEntities db = new MoviesEntities();
//
// GET: /Movies/
public ActionResult Index()
{
var movies = from m in db.Movies
select m;
return View(movies.ToList());
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Movie movie)
{
if (ModelState.IsValid)
{
db.AddToMovies(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
else
return View();//View(movie);
}
}
My Create.aspx:
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Title) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Title) %>
<%: Html.ValidationMessageFor(model => model.Title) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.ReleaseDate) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ReleaseDate) %>
<%: Html.ValidationMessageFor(model => model.ReleaseDate) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Genre) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Genre) %>
<%: Html.ValidationMessageFor(model => model.Genre) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Price) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Price) %>
<%: Html.ValidationMessageFor(model => model.Price) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
Quan điểm của tôi là mạnh mẽ gõ. Đằng sau hiện trường, tại sao cả hai có thể cho tôi kết quả tương tự? Tôi bị bối rối. – xport
Có thể bởi vì trong quan điểm của bạn, bạn chỉ sử dụng những người trợ giúp Html mà theo mặc định trước tiên sẽ nhìn vào yêu cầu POST và sau đó là mô hình khi ràng buộc các giá trị của chúng. Nhưng nếu bạn cố gắng truy cập vào một thuộc tính thủ công của mô hình như '<%: Model.FooProp%>' nó có lẽ sẽ ném một ngoại lệ. Vì vậy, nếu bạn có một cái nhìn mạnh mẽ gõ ** luôn luôn ** vượt qua mô hình. –
Trình trợ giúp Html trong giàn giáo do VWD tạo ra không được đánh máy mạnh? – xport