2010-09-27 29 views
10

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> 

Trả lời

9

Nếu xem bạn đang trở về được mạnh mẽ gõ và sử dụng mô hình sẽ tốt hơn để vượt qua mô hình này. Nếu bạn chỉ đơn giản là return View() và trong chế độ xem bạn cố gắng truy cập vào mô hình, bạn sẽ có thể nhận được một số NullReferenceException nhất.

Sau đây là một mô hình phổ biến:

public class HomeController: Controller 
{ 
    public ActionResult Index() 
    { 
     var model = FetchModelFromRepo(); 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(SomeViewModel model) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(model); 
     }   

     // TODO: update db 
     return RedirectToAction("index"); 
    } 
} 
+0

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

+1

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. –

+0

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

Các vấn đề liên quan