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; }
}
這是模型類。
在此先感謝。
沒有視圖添加控制器...它顯示錯誤.... – Sravani
如何沒有添加視圖?你在問題中提出的觀點代碼是什麼? – Fran
@Sravani:您需要返回用戶最初用來填寫表單的相同視圖。如果這不是'Add.cshtml',那麼這意味着您必須發佈到與表單所在網站不同的網址。那很糟*。不要這樣做。 –