TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
sarojini mishra
NA
192
11.9k
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----------
[HttpPost]
public
ActionResult Index(FileBugModel bug)
{
if
(ModelState.IsValid)
{
string
connStr = ConfigurationManager.ConnectionStrings[
"Conn"
].ConnectionString;
using
(SqlConnection con =
new
SqlConnection(connStr))
{
con.Open();
using
(SqlCommand cmd =
new
SqlCommand(
"InsertFiledBug"
, con))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(
"@EmployeeId"
, bug.EmpId);
cmd.Parameters.AddWithValue(
"@IsCritical"
, bug.IsCritical);
cmd.Parameters.AddWithValue(
"@BugType"
, bug.BugType);
cmd.Parameters.AddWithValue(
"@BugSummary"
, bug.BugSummary);
cmd.Parameters.AddWithValue(
"@BugDescription"
, bug.BugDescription);
cmd.ExecuteScalar();
}
}
return
RedirectToAction(
"Close"
);
//return View("Close");
}
else
{
return
View(bug);
}
}
---------FileBugModel.cs---------
using
System.Collections.Generic;
using
System.ComponentModel.DataAnnotations;
using
System.Linq;
using
System.Web;
namespace
Test_BugTrak.Models
{
public
class
FileBugModel
{
public
int
EmpId {
get
;
set
; }
public
string
EmployeeName {
get
;
set
; }
public
string
EmpFirstName {
get
;
set
; }
public
string
EmpLastName {
get
;
set
; }
public
List Bname {
get
;
set
; }
public
string
IsCritical {
get
;
set
; }
public
string
BugType {
get
;
set
; }
[Required(ErrorMessage =
"Please Enter Bug Summary"
)]
public
string
BugSummary {
get
;
set
; }
public
string
BugDescription {
get
;
set
; }
}
}
-------FileNewBug.cshtml---------
@model Test_BugTrak.Models.FileBugModel
@{
Layout
=
null
;
}
@**@
.error{
color:red;
}
@using (Html.BeginForm("Index", "FileBug", FormMethod.Post))
{
@Html.Label("Critical")
@Html.RadioButtonFor(
m
=
>
m.IsCritical, "Y", new {
value
=
"Y"
})
@Html.RadioButtonFor(
m
=
>
m.IsCritical, "N", new {
value
=
"N"
})
@Html.Label("Bug Type *")
@Html.RadioButtonFor(
m
=
>
m.BugType, "D", new {
value
=
"D"
})
@Html.RadioButtonFor(
m
=
>
m.BugType, "F", new {
value
=
"F"
})
@Html.Label("Assign To")
@Html.DropDownListFor(
m
=
>
m.EmpId, new SelectList(Model.Bname, "EmpId", "EmployeeName"), "Select User")
@Html.Label("Summary *")
@Html.TextBoxFor(
m
=
>
m.BugSummary)
@Html.ValidationMessageFor(
m
=
>
m.BugSummary,"",new {@
class
=
"error"
})
@Html.Label("Description *")
@Html.TextAreaFor(
m
=
>
m.BugDescription)
}
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
Reply
Answers (
2
)
chart not displaying on web form
eventlog.exists not working for setup event log ?