MVC server side validation with ASP.net - asp.net-mvc

I am trying to put server side validation for a textbox in MVC website. Here is what I have:
<% using (Html.BeginForm("WebsiteLinks", "Home", FormMethod.Get))
{%>
<%: Html.ValidationSummary("Please enter valid URL and try again.") %>
<fieldset>
<p>
<%=Html.Label("Please enter URL:") %>
<%=Html.TextBox("url")%>
<%= Html.ValidationMessage("url", "*") %>
<input type="submit" value="Crawl" />
</p>
</fieldset>
<% } %>
And in the controller I have this:
public ActionResult WebsiteLinks(string url)
{
if (Regex.IsMatch(url, #"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"))
{
ViewData["AnchorText"] = url;
return View(new Website(url, "Url"));
}
return RedirectToAction("Index");
}
The validation is working fine, but what I want to achieve is if the data is not valid, if the data is not proper url, I want to redirect to the same default page with a message probably here: <%= Html.ValidationMessage("url", "*") %> but I don't know how to do that.
Edit
After I did all the changes recommended below, I am getting an error in the header of the view page. I have Inherits="ViewPageBase<Home>" where Home is the name of the class, Home.cs in the Models folder.
In the home.cs file I have this:
namespace LAX.Models
{
public class UrlModel
{
[Required]
[DisplayName("Please enter URL:")]
[RegularExpression(#"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?")]
public string Url { get; set; }
}
}
in the controller I have:
[HttpPost]
public ActionResult WebsiteLinks(UrlModel model)
{
/*
if (Regex.IsMatch(url, #"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"))
{
ViewData["AnchorText"] = url;
return View(new Website(url, "Url"));
}
else
{
ModelState.AddModelError("url", "Error URL Format");
}
return RedirectToAction("Index");
*/
if (ModelState.IsValid)
{
ViewData["AnchorText"] = model.Url;
return View(new Website(model.Url, "Url"));
}
return RedirectToAction("Index");
}
and in the view I have:
<% using (Html.BeginForm("WebsiteLinks", "Home", FormMethod.Get))
{%>
<%: Html.ValidationSummary("Please enter valid URL and try again.") %>
<fieldset>
<p>
<%=Html.LabelFor(m => m.Url) %>
<%=Html.TextBoxFor(m => m.Url) %>
<%=Html.ValidationMessageFor(m => m.Url) %>
<input type="submit" value="Crawl" />
</p>
</fieldset>
<% } %>
Here is the error: "The type or namespace name 'Home' could not be found (are you missing a using directive or an assembly reference?)"

public ActionResult WebsiteLinks(string url)
{
if (Regex.IsMatch(url, #"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"))
{
ViewData["AnchorText"] = url;
return View(new Website(url, "Url"));
}
else
{
ModelState.AddModelError("url", "*");
}
return RedirectToAction("Index");
}
or you can make this sexier with DataAnnotations, Model, and a strongly typed View
Model:
public class UrlModel
{
[Required]
[DisplayName("Please enter URL:")]
[RegularExpression(#"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?")]
public string Url { get; set; }
}
Controller:
public ActionResult WebsiteLinks(UrlModel model)
{
if (ModelState.IsValid)
{
ViewData["AnchorText"] = model.Url;
return View(new Website(model.Url, "Url"));
}
return RedirectToAction("Index");
}
View:
<%# Page Language="C#" Inherits="ViewPageBase<UrlModel>" %>
<% using (Html.BeginForm("WebsiteLinks", "Home", FormMethod.Get)) {%>
<%: Html.ValidationSummary("Please enter valid URL and try again.") %>
<fieldset>
<p>
<%=Html.LabelFor(m => m.Url) %>
<%=Html.TextBoxFor(m => m.Url) %>
<%=Html.ValidationMessageFor(m => m.Url) %>
<input type="submit" value="Crawl" />
</p>
</fieldset>
<% } %>

Related

Object reference not set to an instance of an object in MVC

I looked at all of the postings with this error but none helps. I am using MVC with the views in ASPX (C#).
I am updating records in the database after editing the data in simple views. When I process one record at a time (Controller EDIT methods and EDIT view) all works fine.
When I process multiple records (Controller UpdateTest and UPDATETEST view) I get an error in my HTTPPOST method in Controller after clicking the Submit button in a view.
here is an error with the sample codes one which works and one which does not:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: (CONTROLLER – SEE BELOW HttpPost UpdateTest(ICollection<Mysurvey> mysurveys)
enter code here
Line 48: public ActionResult UpdateTest(ICollection<Mysurvey> mysurveys)
Line 49: {
Line 50: foreach (var survey in mysurveys)
Line 51: {
Line 52: db.Entry(survey).State = EntityState.Modified;
Source File: C:\Users\rsc_vok\Documents\Visual Studio 2010 \Projects\MvcMysurvey\MvcMysurvey\Controllers\MysurveyController.cs Line: 50
CONTROLLER
namespace MvcMysurvey.Controllers
{
THE CODE WHICH WORKS – SINGLE EDIT
public ActionResult Edit(int id)
{
Mysurvey mysurvey = db.Mysurveys.Find(id);
return View(mysurvey);
}
// POST: /Mysurvey/Edit/5
[HttpPost]
public ActionResult Edit(Mysurvey mysurvey)
{
System.Diagnostics.Debug.WriteLine("iam in edit post");
if (ModelState.IsValid)
{
db.Entry(mysurvey).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(mysurvey);
}
THE CODE WITH THE ERROR IN httppost – MULTIPLE RECORDS SAVE
public ActionResult UpdateTest(int id = 0)
{
List<Mysurvey> mysurveys = db.Mysurveys.ToList();
return View(mysurveys.ToList());
}
[HttpPost]
public ActionResult UpdateTest(ICollection<Mysurvey> mysurveys)
{
foreach (var survey in mysurveys)
{
db.Entry(survey).State = EntityState.Modified;
}
db.SaveChanges();
return View(mysurveys);
}
VIEW
UPDATETEST
THE CODE WHICH WORKS HERE BUT THROWS AN ERROR IN CONTROLLER AFTER THE SAVE BUTTON IS HIT
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcMysurvey.Models.Mysurvey>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Updatetest
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>
<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Mysurvey</legend>
<% foreach (var item in Model) { %>
<%: Html.HiddenFor(model => item.ID) %>
<%: Html.HiddenFor(model => item.SurveyID) %>
<div class="editor-label">
<%: Html.LabelFor(model => item.Comment) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => item.Comment) %>
<%: Html.ValidationMessageFor(model => item.Comment) %>
</div>
<%} %>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
MODEL
namespace MvcMysurvey.Models
{
public class Mysurvey
{
public int ID { get; set; }
[DisplayFormat(DataFormatString = "{0:c}")]
public int SurveyID { get; set; }
public string Comment { get; set; }
}
public class MysurveyDBContext : DbContext
{
public DbSet<Mysurvey> Mysurveys { get; set; }
}
}

How add data in asp.net mvc?

If I add data to table witch has no relationships, it's all good: data is adding. But if table have relationships, this is something wrong
Here is my project, what i mean is, for example AddSt in RouteController.
http://zalil.ru/32249903
Here is controller:
[HttpGet]
public ActionResult AddSt(int RouteId)
{
var routeDetails = (from rd in db.Route
join rdd in db.RouteDetail
on rd.RouteId equals rdd.Route.RouteId ///check
where rd.RouteId == RouteId
select rdd).FirstOrDefault();
return View(routeDetails);
}
[HttpPost]
public ActionResult AddSt(RouteDetail rd)
{
try
{
if (ModelState.IsValid)
{
db.AddToRouteDetail(rd);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (Exception e)
{
ModelState.AddModelError("Error!", e);
}
return View();
}
and view:
<% using (Html.BeginForm("AddSt","Route")) {%>
<%= Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Route.RouteId)%>
<%= Html.TextBoxFor(model => model.Station)%>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
Why I can't write model => model.RouteId ????
What's wrong?
Why TrainSheduleDBEntities table RouteDetail doesn't generate field RouteID ?
You are only selecting RouteDetail (rdd)
So you want model.RouteId
Just stick a debug on the addst action.
Have a look what's in rd.
I'm guessing that there's no valid routeid in it.

Custom validation rules for ASP.NET MVC2 Application

I am attempting to add validation to my application. I have some rules I need to check before allowing the information to be written to the database. I have the basic data validation added to the model, but I also need to make sure that if one field has a certain value, this other field is required. At one time the NerdDinner tutorial at asp.net covered that and I used that in the past for validation, but now I can't find that or any other example. Here is my model:
public class DayRequested
{
public int RequestId { set; get; }
[Required, DisplayName("Date of Leave")]
public string DateOfLeave { get; set; }
[Required, DisplayName("Time of Leave")]
public string TimeOfLeave { get; set; }
[Required, DisplayName("Hours Requested")]
[Range(0.5, 24, ErrorMessage = "Requested Hours must be within 1 day")]
public double HoursRequested { get; set; }
[Required, DisplayName("Request Type")]
public string RequestType { get; set; }
[DisplayName("Specify Relationship")]
public string Relationship { get; set; }
[DisplayName("Nature of Illness")]
public string NatureOfIllness { get; set; }
public bool AddedToTimesheet { get; set; }
public bool IsValid
{
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty(DateOfLeave))
yield return new RuleViolation("Date of Leave Required", "DateOfLeave");
if (String.IsNullOrEmpty(TimeOfLeave))
yield return new RuleViolation("Date of Leave Required", "TimeOfLeave");
if ((HoursRequested < 0.5) || (HoursRequested > 24))
yield return new RuleViolation("Hours must be in a period of one day", "HoursRequested");
if (String.IsNullOrEmpty(RequestType))
yield return new RuleViolation("Request Type is required", "RequestType");
if ((!String.IsNullOrEmpty(NatureOfIllness)) && (NatureOfIllness.Length < 3))
yield return new RuleViolation("Nature of Illness must be longer 2 characters", "NatureOfIllness");
// Advanced data validation to make sure rules are followed
LeaveRequestRepository lrr = new LeaveRequestRepository();
List<LeaveRequestType> lrt = lrr.GetAllLeaveRequestTypes();
LeaveRequestType workingType = lrt.Find(b => b.Id == Convert.ToInt32(RequestType));
if ((String.IsNullOrEmpty(Relationship)) && (workingType.HasRelationship))
yield return new RuleViolation("Relationship is Required", "Relationship");
if ((String.IsNullOrEmpty(NatureOfIllness)) && (workingType.HasNatureOfIllness))
yield return new RuleViolation("Nature of Illness is Required", "NatureOfIllness");
yield break;
}
}
My controller:
//
// POST: /LeaveRequest/Create
[Authorize, HttpPost]
public ActionResult Create(LeaveRequest leaveRequest, List<DayRequested> requestedDays)
{
if (ModelState.IsValid)
{
foreach (DayRequested requestedDay in requestedDays)
{
requestedDay.RequestId = leaveRequest.RequestId;
requestedDay.NatureOfIllness = (String.IsNullOrEmpty(requestedDay.NatureOfIllness) ? "" : requestedDay.NatureOfIllness);
requestedDay.Relationship = (String.IsNullOrEmpty(requestedDay.Relationship) ? "" : requestedDay.Relationship);
if (requestedDay.IsValid)
lrRepository.CreateNewLeaveRequestDate(requestedDay);
else
return View(new LeaveRequestViewModel(leaveRequest, requestedDays, lrRepository.GetLeaveRequestTypes()));
}
if (leaveRequest.IsValid)
lrRepository.CreateNewLeaveRequest(leaveRequest);
else
return View(new LeaveRequestViewModel(leaveRequest, requestedDays, lrRepository.GetLeaveRequestTypes()));
}
else
return View(new LeaveRequestViewModel(leaveRequest, requestedDays, lrRepository.GetLeaveRequestTypes()));
return RedirectToAction("Index", lrRepository.GetLeaveRequests(udh.employeeId));
}
ModelState.IsValid is not set to false though the code in IsValid is run and does return a RuleViolation. So I manually check IsValid it returns false. When I return to the view, the error messages do not appear. What might I be missing? Here are some snippets of the views.
Create.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create New Leave Request</h2>
<div><%= Html.ActionLink("Back to List", "Index") %></div>
<%= Html.Partial("RequestEditor", Model) %>
<div><%= Html.ActionLink("Back to List", "Index") %></div>
</asp:Content>
RequestEditor.ascx
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<table id="editorRows">
<% foreach (var item in Model.DaysRequested)
Html.RenderPartial("RequestedDayRow", new EmployeePayroll.ViewModels.LeaveRequestRow(item, Model.LeaveRequestType)); %>
</table>
<p>Type your time to sign your request.</p>
<p><%= Html.LabelFor(model => model.LeaveRequest.EmployeeSignature) %>:
<%= Html.TextBoxFor(model => model.LeaveRequest.EmployeeSignature, new { Class="required" })%>
<%= Html.ValidationMessageFor(model => model.LeaveRequest.EmployeeSignature)%></p>
<p><input type="submit" value="Submit Request" /></p>
<% } %>
RequestedDayRow.ascx
<tbody class="editorRow">
<tr class="row1"></tr>
<tr class="row2">
<td colspan="2" class="relationship">
<%= Html.LabelFor(model => model.DayRequested.Relationship)%>:
<%= Html.TextBoxFor(model => model.DayRequested.Relationship) %>
<%= Html.ValidationMessageFor(model => model.DayRequested.Relationship)%>
</td>
<td colspan="2" class="natureOfIllness">
<%= Html.LabelFor(model => model.DayRequested.NatureOfIllness)%>:
<%= Html.TextBoxFor(model => model.DayRequested.NatureOfIllness) %>
<%= Html.ValidationMessageFor(model => model.DayRequested.NatureOfIllness)%>
</td>
<td></td>
</tr>
</tbody>
It's quite simple - you just need to apply your validation attribute to the entire model (or a child class). Then the validation attribute gets a reference to the model instead of just one property and you can perform your checks on multiple properties.
You should look at password validation for an example of how to do this.
Check out the PropertiesMustMatch validator here:
http://msdn.microsoft.com/en-us/magazine/ee336030.aspx

MVC Validation Not Working In Web Forms Project

I have the following code in my aspx view page:
<% using (Html.BeginForm())
{
%>
<div>
CustomerCode:
<%= Html.TextBoxFor(x=> x.CustomerCode) %>
<%= Html.ValidationMessageFor(x => x.CustomerCode)%>
and this code in my model:
public class MyModel
{
[Required(ErrorMessage="customer code req")]
[StringLength(2,ErrorMessage="must be 2 u idiot")]
public string CustomerCode {get; set;}
Though if I enter more than 2 charachters in the textbox and submit the page, in the controller when I do:
if (ModelState.IsValid)
It always says its valid? What am I missing? I have put this MVC project inside a Web Forms project but the MVC project works fine, its just the validation which is not working, any ideas? Thanks.
Make sure that the controller action accepts the model as parameter:
public ActionResult SomeAction(MyModel model)
{
if (ModelState.IsValid)
{
}
return View();
}
Now if you invoke:
http://example.com/myapp/home/someaction?customercode=123
The model should not be valid.
Hmm, it works for me on a test page with the following
public ActionResult Test()
{
MyModel model = new MyModel();
return View(model);
}
[HttpPost]
public ActionResult Test(MyModel model)
{
if (ModelState.IsValid) { }
return View(model);
}
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.CustomerCode) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.CustomerCode) %>
<%: Html.ValidationMessageFor(model => model.CustomerCode) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
public class MyModel
{
[Required(ErrorMessage = "customer code req")]
[StringLength(2, ErrorMessage = "must be 2 u idiot")]
public string CustomerCode { get; set; }
}

Why do Validation Errors persist?

A user submits registration information to the form:
<h2>Create</h2>
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Nbk">Nbk:</label>
<%= Html.TextBox("Nbk",null, new {Disabled="Disabled" })%>
<%= Html.ValidationMessage("Nbk", "*") %>
</p>
<p>
<label for="Name">Name:</label>
<%= Html.TextBox("Name") %>
<%= Html.ValidationMessage("Name", "*") %>
</p>
<p>
<label for="Email">Email:</label>
<%= Html.TextBox("Email") %>
<%= Html.ValidationMessage("Email", "*") %>
</p>
<p>
<label for="MailCode">MailCode:</label>
<%= Html.TextBox("MailCode") %>
<%= Html.ValidationMessage("MailCode", "*") %>
</p>
<p>
<label for="TelephoneNumber">TelephoneNumber:</label>
<%= Html.TextBox("TelephoneNumber") %>
<%= Html.ValidationMessage("TelephoneNumber", "*") %>
</p>
<p>
<label for="OrganizationId">OrganizationId:</label>
<%= Html.TextBox("OrganizationId") %>
<%= Html.ValidationMessage("OrganizationId", "*") %>
</p>
<p>
<label for="OrganizationSponsorId">OrganizationSponsorId:</label>
<%= Html.TextBox("OrganizationSponsorId") %>
<%= Html.ValidationMessage("OrganizationSponsorId", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
Controller:
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(DefectSeverityAssessmentBusiness.ModelRegistration registration)
{
registration.Nbk = StateController.GetNbk(Request);
try
{
var errors = DataAnnotationsValidationRunner.GetErrors(registration);
if (errors.Any())
foreach (var item in errors)
{
if( ModelState[item.PropertyName].Errors.Count==0)
ModelState.AddModelError(item.PropertyName, item.ErrorMessage);
//ModelState.SetModelValue(item.PropertyName,ViewData[item.PropertyName].ToValueProvider());
}
if (ModelState.IsValid)
{
_RegistrationRepository.CreateRegistration(registration);
return RedirectToAction("Index", "Assessment");
}
}
catch (Exception exception)
{
ModelState.AddModelError("Exception", exception);
}
return View();
}
controller factory:
ControllerBuilder.Current.SetControllerFactory(new Models.InMemoryRepositories.InMemoryControllerFactory());
public class InMemoryControllerFactory : IControllerFactory
{
private readonly Dictionary<string, IController> _controllers = new Dictionary<string, IController>();
private readonly Dictionary<string, Func<IController>> _controllerFactoryDictionary =
new Dictionary<string, Func<IController>>();
public InMemoryControllerFactory()
{
InitializeDictionary();
}
private void InitializeDictionary()
{
AddFactory(typeof(Controllers.HomeController), () => new Controllers.HomeController(
new Models.InMemoryRepositories.Registration.InMemoryRegistrationRepository()));
AddFactory(typeof(Controllers.RegistrationController),() => new Controllers.RegistrationController(
new Models.InMemoryRepositories.Registration.InMemoryRegistrationRepository()));
AddFactory(typeof(Controllers.AssessmentController),()=> new Controllers.AssessmentController(
new Models.InMemoryRepositories.Registration.InMemoryDefectRepository(),
new Models.InMemoryRepositories.Registration.InMemoryAssessmentRepository())
);
}
private void AddFactory(Type type, Func<IController> creator)
{
const string Str_Controller = "Controller";
var fullname = type.Name;
Debug.Assert(fullname.EndsWith(Str_Controller));
var controllerName= fullname.Substring(0, fullname.Length - Str_Controller.Length);
Func<string, Func<IController>> controllerFactoryFunc = (_controllerName) =>
{
return () =>
{
//var controllerName=ControllerNameFunc(type);
if (!_controllers.ContainsKey(_controllerName))
_controllers.Add(_controllerName, creator());
return _controllers[_controllerName];
};
};
_controllerFactoryDictionary.Add(controllerName, controllerFactoryFunc(controllerName));
}
#region IControllerFactory Members
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
return _controllerFactoryDictionary[controllerName]();
}
/// <summary>
/// Code via http://nayyeri.net/custom-controller-factory-in-asp-net-mvc
/// </summary>
/// <param name="controller"></param>
public void ReleaseController(IController controller)
{
if (controller is IDisposable)
(controller as IDisposable).Dispose();
else
controller = null;
}
#endregion
}
Through the controller with an invalid value at first, then a valid one, but the error message stays and modelstate stays invalid. Why does this happen? i'm using the default model binder, but the included controller factory.
A Controller should be instantiated per request. Think of the ControllerContext being much like the HttpContext. It represents the state of a single request. Since you're storing controllers in a static dictionary, you're not clearing the state of the controller for each request, thus it holds onto its modelstate.
In general, you should not use the Singleton pattern with controllers because of this. Otherwise you're responsible for clearing its state on each request. You're probably not getting much of a perf benefit anyways by doing that.

Resources