2017-02-24 51 views
0
public ActionResult Add(Models.ContactModel contact) 
    { 
     if (ModelState.IsValid) 
     { 
      DAL.Contact mappedContact = Mapper.Map<Models.ContactModel, DAL.Contact>(contact); 
      repository.AddContact(mappedContact); 
      return RedirectToAction("Index"); 
     } 
     else 
      /* What to return here */         
    } 

這是用於將聯繫人添加到數據庫的控制器。我正在使用數據註釋驗證表單,如果表單有效,我將它重定向到索引頁面。如果它無效,它應該保持在顯示錯誤信息的同一頁面上。在其他部分寫什麼。任何人都可以建議我。對於添加控制器沒有看法。如果表格無效,請保持在同一頁面

<div> 
      <label>Name</label> 
      @Html.ValidationMessageFor(model => model.Name, null, new { @class = "error-message"}) 
      @Html.TextBoxFor(model => model.Name, new { @class = "long-box" }) 
     </div> 
     <div> 
      <label>Email</label> 
      @Html.ValidationMessageFor(model => model.Email, null, new { @class = "error-message" }) 
      @Html.TextBoxFor(model => model.Email, new { @class = "long-box" }) 
     </div> 
     <div class="mob-land-container"> 
      <label>Mobile</label> 
      @Html.ValidationMessageFor(model => model.MobileNumber, null, new { @class = "error-message" }) <br> 
      @Html.TextBoxFor(model => model.MobileNumber, new { @class = "short-box" }) 
     </div> 
     <div class="mob-land-container" id="landline-container"> 
      <label>Landline</label> 
      @Html.ValidationMessageFor(model => model.LandlineNumber, null, new { @class = "error-message" })<br> 
      @Html.TextBoxFor(model => model.LandlineNumber, new { @class = "short-box" }) 
     </div> 
     <div> 
      <label>Website</label> 
      @Html.ValidationMessageFor(model => model.Website, null, new { @class = "error-message" }) 
      @Html.TextBoxFor(model => model.Website, new { @class = "long-box" }) 
     </div> 
     <div> 
      <label>Address</label> 
      @Html.ValidationMessageFor(model => model.Address, null, new { @class = "error-message" }) 
      @Html.TextAreaFor(model => model.Address, new { @class = "address-box" }) 
     </div> 
    </div> 
    <div class="button-container"> 
     <input type="button" id="cancel" value="Cancel" onclick="location.href='@Url.Action("Index", "Contact")'" /> 
     <input type="submit" id="add" value="Add" onclick="location.href='@Url.Action("Add", "Contact")'" /> 
    </div> 

這是我得到數據到控制器的形式。

public class ContactModel 
{ 
    public int Id { get; set; } 
    [Required(ErrorMessage = "Name is required")] 
    public string Name { get; set; } 
    [Required(ErrorMessage = "Email is required")] 
    public string Email { get; set; } 
    [Required(ErrorMessage = "Mobile Number is required")] 
    public string MobileNumber { get; set; } 
    [Required(ErrorMessage = "Landline Number is required")] 
    public string LandlineNumber { get; set; } 
    [Required(ErrorMessage = "Website is required")] 
    public string Website { get; set; } 
    [Required(ErrorMessage = "Address is required")] 
    public string Address { get; set; } 
} 

這是模型類。

在此先感謝。

回答

0

我喜歡在這種情況下翻轉登錄。如果模型無效,請將其返回到視圖。 POST上的模型聯編程序將負責驗證,一旦將模型發送回視圖,您將在屏幕上看到單個驗證。

如果您有任何下拉菜單,您需要在發送模型之前重新填充它們。

public ContactController : Controller 
{ 
    [HttpGet] 
    public ActionResult Add() 
    { 
     return View(new Models.ContactModel()); 
    } 

    [HttpPost] 
    public ActionResult Add(Models.ContactModel contact) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(contact); 
     } 

     DAL.Contact mappedContact = Mapper.Map<Models.ContactModel, DAL.Contact>(contact); 
     repository.AddContact(mappedContact); 
     return RedirectToAction("Index");       
    } 
} 

GET操作返回空格式。 POST操作將模型發佈到服務器。

您的視圖模型應該命名爲Add.cshtml,以便mvc可以自動選取它。

,並更改您的視圖按鈕

<div class="button-container"> 
     @Html.ActionLink("Cancel", "Index", "Contact") 
     <input type="submit" value="Save" /> 
    </div> 

風格的取消鏈接看起來像一個按鈕 您的提交將自動提交到添加POST方法。

模型狀態檢查將模型返回到包含驗證信息的視圖,以便您可以更正表單。

+0

沒有視圖添加控制器...它顯示錯誤.... – Sravani

+0

如何沒有添加視圖?你在問題中提出的觀點代碼是什麼? – Fran

+1

@Sravani:您需要返回用戶最初用來填寫表單的相同視圖。如果這不是'Add.cshtml',那麼這意味着您必須發佈到與表單所在網站不同的網址。那很糟*。不要這樣做。 –

相關問題