2017-02-24 53 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',那么这意味着您必须发布到与表单所在网站不同的网址。那很糟*。不要这样做。 –

相关问题