sarojini mishra

sarojini mishra

  • NA
  • 192
  • 11.5k

Validation in MVC

Nov 24 2017 5:34 AM
Hello friends,
 
I am new in MVC.
I have created a grid and fetch data from database into it.
For Adding record I have opened a window on button click and save data in database.
Now I am trying to validate the controls but some how it is not working.
Kindly guide me friends on this.
 
--------- FileBugController.cs----------
  1. [HttpPost]  
  2. public ActionResult Index(FileBugModel bug)  
  3. {  
  4. if(ModelState.IsValid)  
  5. {  
  6. string connStr = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;  
  7. using (SqlConnection con = new SqlConnection(connStr))  
  8. {  
  9. con.Open();  
  10. using (SqlCommand cmd = new SqlCommand("InsertFiledBug", con))  
  11. {  
  12. cmd.CommandType = System.Data.CommandType.StoredProcedure;  
  13. cmd.Parameters.AddWithValue("@EmployeeId", bug.EmpId);  
  14. cmd.Parameters.AddWithValue("@IsCritical", bug.IsCritical);  
  15. cmd.Parameters.AddWithValue("@BugType", bug.BugType);  
  16. cmd.Parameters.AddWithValue("@BugSummary", bug.BugSummary);  
  17. cmd.Parameters.AddWithValue("@BugDescription", bug.BugDescription);  
  18. cmd.ExecuteScalar();  
  19. }  
  20. }  
  21. return RedirectToAction("Close");  
  22. //return View("Close");  
  23. }  
  24. else  
  25. {  
  26. return View(bug);  
  27. }  
  28. }  
---------FileBugModel.cs---------
  1. using System.Collections.Generic;  
  2. using System.ComponentModel.DataAnnotations;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace Test_BugTrak.Models  
  6. {  
  7. public class FileBugModel  
  8. {  
  9. public int EmpId { getset; }  
  10. public string EmployeeName { getset; }  
  11. public string EmpFirstName { getset; }  
  12. public string EmpLastName { getset; }  
  13. public List Bname { getset; }  
  14. public string IsCritical { getset; }  
  15. public string BugType { getset; }  
  16. [Required(ErrorMessage = "Please Enter Bug Summary")]  
  17. public string BugSummary { getset; }  
  18. public string BugDescription { getset; }  
  19. }  
  20. }  
-------FileNewBug.cshtml---------
  1. @model Test_BugTrak.Models.FileBugModel  
  2. @{  
  3. Layout = null;  
  4. }  
  5. @**@  
  6. .error{  
  7. color:red;  
  8. }  
  9. @using (Html.BeginForm("Index", "FileBug", FormMethod.Post))  
  10. {  
  11.   
  12. @Html.Label("Critical")  
  13. @Html.RadioButtonFor(m => m.IsCritical, "Y", new { value = "Y"})  
  14. @Html.RadioButtonFor(m => m.IsCritical, "N", new { value = "N"})  
  15. @Html.Label("Bug Type *")  
  16. @Html.RadioButtonFor(m => m.BugType, "D", new { value = "D"})  
  17. @Html.RadioButtonFor(m => m.BugType, "F", new { value = "F"})  
  18. @Html.Label("Assign To")  
  19. @Html.DropDownListFor(m => m.EmpId, new SelectList(Model.Bname, "EmpId", "EmployeeName"), "Select User")  
  20. @Html.Label("Summary *")  
  21. @Html.TextBoxFor(m => m.BugSummary)  
  22. @Html.ValidationMessageFor(m => m.BugSummary,"",new {@class="error" })  
  23. @Html.Label("Description *")  
  24. @Html.TextAreaFor(m => m.BugDescription)  
  25. }  
Following error occur after ModelState.IsValid check in else part.
 
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
 
~/Views/FileBug/Index.aspx
~/Views/FileBug/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/FileBug/Index.cshtml
~/Views/FileBug/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
 
Image of my Solution Explorer. Maybe I have mistaken here. Kindly help me out.
 
 
 
This Is screenshot of my Solution Explorer

Attachment: MVC_BUG_SAMPLE.zip

Answers (2)