2010-12-09 55 views
0

我是MVC的初學者。我創建了一個包含下拉列表,文本框和文本區域的視圖。我沒有創建強類型視圖,因爲這些字段不是從數據庫生成的。這是正確的方式嗎?我是否必須創建一個'Model'類並生成強類型視圖?我試圖驗證(客戶端和服務器端)這些fieds之前保存到數據庫中。如何爲這些字段創建一個Model類?我很欣賞任何輸入。如何創建一個Model類來驗證表單域

謝謝..

這是我的看法。

Create.aspx

<% using (Html.BeginForm()) { %> 
    <div> 
    <label for="Reason">Reason:</label> 
    </div> 
    <div> 
    <%= Html.DropDownList("lstReasons", new SelectList((IEnumerable)ViewData["ReasonList"], "ReasonID", "ReasonDetail"), "Select Reason")%> 
    </div> 

    <div> 
      <label for="StartDate">Start Date:</label> 
    </div> 
    <div> 
      <%= Html.TextBox("StartDate")%> 

    </div> 

      <div>   
      <label for="Details">Details:</label> 
      </div> 
      <div> 
      <%=Html.TextArea("Details")%> 
      </div> 

    <input type="submit" value="Submit" /> 
    <input type="reset" value="Cancel" /> 
<% } %> 

我家的控制器代碼。

public ActionResult Create() 
    { 
     var reasons = from reason in db.Reasons 
         select reason; 

     ViewData["ReasonList"] = reasons; 
    } 

回答

2

我想我明白你正在嘗試做的,這裏是我會做一個例子:

創建一個使用數據註釋來執行客戶端驗證(爲必填字段和視圖模型字符串長度等):

public class CreateViewModel 
{ 
    public SelectList Reasons { get; set; } //to store list of reasons from db 
    public string Reason { get; set; } //to store selected reason 

    [Required] 
    public DateTime StartDate {get; set;} 

    [Required] 
    [StringLength(250, ErrorMessage = "Details must be less than 250 characters")] 
    public string Details { get; set; } 
} 

具有被強類型到DetailsViewModel並使用Html.EnableClientValidation以接通客戶端驗證和Html.ValidationMessageFor顯示驗證消息的視圖:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.CreateViewModel>" %> 
<% Html.EnableClientValidation(); %> 
<% Html.ValidationSummary(); %> 

<% using (Html.BeginForm()) { %> 
    <div> 
     <label for="Reason">Reason:</label> 
    </div> 
    <div> 
     <%= Html.DropDownListFor(m => m.Reason,Model.Reasons, "Select Reason")%> 
    </div> 

    <div> 
     <label for="StartDate">Start Date:</label> 
    </div> 
    <div> 
     <%= Html.TextBoxFor(m => m.StartDate)%> 
     <%= Html.ValidationMessageFor(m => m.StartDate %> 
    </div> 

    <div>   
     <label for="Details">Details:</label> 
    </div> 
    <div> 
     <%=Html.TextAreaFor(m => m.Details)%> 
     <%= Html.ValidationMessageFor(m => m.Details %> 
    </div> 

<input type="submit" value="Submit" /> 
<input type="reset" value="Cancel" /> 

<%}%>

而在你的控制器的方法Get方法來顯示視圖:

[HttpGet] 
    public ActionResult Create() 
    { 
     var viewModel = new CreateViewModel(); 

     //get reasons from DB 
     var reasons = from reason in db.Reasons 
         select reason; 

     //set the reason Id as the unique identifier of the reason and the reason text to be what will be displayed in the dropdown 
     var reasonItems = reasons.Select(r => new { ShortCode = reasons.Id, Definition = reasons.reasonText }); 

     //create a select list that will select the id as the value and show the definition in the label 
     viewModel.Reasons = new SelectList(reasonItems, "ShortCode", "Definition"); 

     return View("", viewModel); //return the populated dropdown to the view 
    } 

,並在控制器Post方法檢查使用ModelState.IsValid,以檢查是否視圖數據是和如果不添加模型狀態錯誤出現在查看頁面上:

[HttpPost] 
    public ActionResult Create(CreateViewModel viewModel) 
    { 
     if (ModelState.IsValid) 
     { 
      // go and save your view model data 
     } 
     ModelState.AddModelError("Error", "Values are not valid"); 

     return RedirectToAction("Create"); 
    } 

在這種情況下,我只需添加一個模型狀態錯誤,然後重定向到控制器Get方法,該方法將再次顯示包含模型錯誤的視圖(這是視圖中的Html.ValidationSummary代碼的作用 - 自動顯示模型狀態錯誤)。

我希望這會有所幫助。

+0

非常感謝你的詳細解釋。到目前爲止它效果很好。我試圖將這個表格保存在3個不同的表格中。我必須在表A中保存原因。我必須從表A中獲取自動生成的ID,並在表B中保存此ID,開始日期和詳細信息。再次非常感謝。 – nav100 2010-12-10 17:10:23

相關問題