Custom validation rules for ASP.NET MVC2 Application - asp.net-mvc

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

Related

MVC server side validation with ASP.net

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>
<% } %>

ViewModel validation object issue with L2SQL generated model class

i'm having issues with implementing a ViewModel class which acts as a wrapper class around a Model class called "User" generated by L2SQL. In short, I have two instances when i have to validate against the L2SQL model. One when the user first signs up, and the second when the user logs in and edits only part of his account data.
The initial "problem" was when trying to edit & validate only some the account details against the original L2SQL model from a View which only displays the few (e.g FirstName, LastName, Email), the missing ones (e.g. Password) would flair up when validation was run. The Password settings will have its own View.
I was advised here on StackOverflow that adding a wrapping ViewModel class would be the best solution. So I did, implemented below:
ViewModel code:
[Bind]
public class AccountEdit
{
public User UserAccount { get; set; }
[Required(ErrorMessage = "First name required"), StringLength(20, MinimumLength = 3, ErrorMessage = "Must be between 3 and 20 characters")]
public string FirstName { get { return UserAccount.FirstName; } set { UserAccount.FirstName = value; } }
[Required(ErrorMessage = "Last name required"), StringLength(20, MinimumLength = 3, ErrorMessage = "Must be between 3 and 20 characters")]
public string LastName { get { return UserAccount.LastName; } set { UserAccount.LastName = value; } }
[Required(ErrorMessage = "Email address required"), RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*#[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Must be a valid email address")]
public string Email { get { return UserAccount.Email; } set { UserAccount.Email = value; } }
}
Controller code:
//
// GET /User/Account
public ActionResult Account()
{
string cookieUser = User.Identity.Name;
User user = userRepository.GetUserByEmail(cookieUser);
return View(user);
}
// POST /User/Account
[HttpPost]
public ActionResult Account(AccountEdit model)
{
if (ModelState.IsValid)
{
model.LastUpdated = DateTime.Now;
userRepository.Save();
}
return View(model);
}
Veiw code:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/User.Master" Inherits="System.Web.Mvc.ViewPage<Digitalent.Models.User>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Account settings
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary("Oops! Please correct the errors and try again!") %>
<p>
<label for="FirstName">First name:</label>
<%: Html.TextBoxFor(model => model.FirstName, new { #class = "textfield" })%>
<%: Html.ValidationMessageFor(model => model.FirstName) %>
</p>
<p>
<label for="LastName">Last name:</label>
<%: Html.TextBoxFor(model => model.LastName, new { #class = "textfield" })%>
<%: Html.ValidationMessageFor(model => model.LastName) %>
</p>
<p>
<label for="Email">Email address:</label>
<%: Html.TextBoxFor(model => model.Email, new { #class = "textfield" })%>
<%: Html.ValidationMessageFor(model => model.Email) %>
</p>
<p>
Email newsletter: <%= Html.CheckBoxFor(model => model.EmailNewsletter) %>
<label for="EmailNewsletter">Keep me posted with latest Digitalent happenings.</label>
</p>
<p>
<input type="submit" value="Save changes" class="button" />
</p>
<p>Change your password settings</p>
<% } %>
But now when i tried to run the app i get an error when it runs the validation:
'Object reference not set to an instance of an object.'
On the line below in my ViewModel:
'public string FirstName { get { return UserAccount.FirstName; } set { UserAccount.FirstName = value; } }'
Where am I going wrong or can anyone else tell me a better recommended approach as i'm a novice. Help please!
Change your view from:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/User.Master"
Inherits="System.Web.Mvc.ViewPage<Digitalent.Models.User>" %>
to:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/User.Master"
Inherits="System.Web.Mvc.ViewPage<Digitalent.Models.AccountEdit>" %>

DropDownListFor not binding on Edit View with repeating items (List<T>)

Here is the thing. I have an Edit view, which doesnt bind the dropdowns' value when I open it.
[NonAction]
public List<SelectListItem> VraagType() {
List<SelectListItem> l = new List<SelectListItem>();
SelectListItem a = new SelectListItem();
SelectListItem b = new SelectListItem();
a.Text = "Meerkeuze";
a.Value = "M";
b.Text = "Open";
b.Value = "O";
l.Add(a);
l.Add(b);
return l;
}
[NonAction]
public List<SelectListItem> getSchalen() {
return _db.EvalSchaals.ToList().ToSelectList(q => q.Sch_Naam, q => q.Sch_ID.ToString(), q => q.Sch_ID == -1).ToList();
}
public ActionResult Edit(int id) {
ViewData["vraagtype"] = VraagType();
ViewData["schaal"] = getSchalen();
EvalVragenBlok evb = _db.EvalVragenBloks.First(q => q.Vrbl_ID == id);
List<EvalVragen> ev = _db.EvalVragens.Where(q => q.Vrbl_ID == id).ToList();
FlatEvalVragenBlok fevb = Mapper.Map<EvalVragenBlok, FlatEvalVragenBlok>(evb);
fevb.Vragen = new List<FlatEvalVragen>();
return View(fevb);
}
this is the code from the controller.
here is the code from the Edit.aspx view
<h2>
Edit</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Vrbl_Titel) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Vrbl_Titel) %>
<%: Html.ValidationMessageFor(model => model.Vrbl_Titel) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Sch_ID) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Sch_ID, ViewData["schaal"] as List<SelectListItem>, "Selecteer een schaal...") %>
<%: Html.ValidationMessageFor(model => model.Sch_ID) %>
</div>
<%= Html.ValidationMessageFor(model => model.Vragen) %>
<table id="vragentbl">
<tr>
<th>
</th>
<th>
Vraag
</th>
<th>
Soort
</th>
</tr>
<% if (Model.Vragen != null) { %>
<% for (int i = 0; i < Model.Vragen.Count; i++) { %>
<tr>
<td>
<%=i + 1%>
</td>
<td>
<%= Html.TextBoxFor(model => model.Vragen[i].Evvr_Vraag, new { style = "width:400px" })%><br />
<%= Html.ValidationMessageFor(model => model.Vragen[i].Evvr_Vraag)%>
</td>
<td>
<%= Html.DropDownListFor(model => model.Vragen[i].Evvr_Type, ViewData["vraagtype"] as List<SelectListItem>, new { style = "width:95px" })%><br />
<%= Html.ValidationMessageFor(model => model.Vragen[i].Evvr_Type)%>
</td>
</tr>
<% }
} %>
<tr>
<td>
</td>
<td>
<a id="addnew" href="#">Voeg extra keuze toe</a>
</td>
<td>
</td>
</tr>
</table>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
I have 2 List 's. 1 of them is in the non-repeating part of the form (Schalen), the other one (VraagType) is Inside the repeating part.
for Schalen, everything works fine. i open the edit view, and all fields are filled in like it should be. the Vrbl_Titel has its value, and the dropdown of Sch_ID has the value it received from the object which i sent with the view, which came from the DB.
The problem lies in the repeating part.
the textbox for model.Vragen[i].Evvr_Vraag get's its value, and the dropdown for model.Vragen[i].Evvr_Type is shown, however, this dropdown does not get the value which was sent in the object. it keeps it's default standard value, which is the first item in the 'selectlist'
how do i get my value from my 'Vragen' object, into the dropdown. if i put the value in a simple textbox
<%= Html.TextBoxFor(model => model.Vragen[i].Evvr_Type)%>
then the textbox does get the value. so the problem is that the dropdownvalue doesnt change form it's initial value... bug in MVC?
just for info, this is how the object(s) look sent to the view:
namespace MVC2_NASTEST.Models {
public partial class FlatEvalVragenBlok {
public int Vrbl_ID { get; set; }
public int Sch_ID { get; set; }
public string Vrbl_Titel { get; set; }
public List<FlatEvalVragen> Vragen { get; set; }
}
}
namespace MVC2_NASTEST.Models {
public partial class FlatEvalVragen {
public int Evvr_ID { get; set; }
public int Vrbl_ID { get; set; }
public int Evvr_rang { get; set; }
public string Evvr_Vraag { get; set; }
public char Evvr_Type { get; set; }
}
}
It seems this is really a bug or at least inconsistency in ASP.NET MVC 2. I have examined its source and found what InputHelper() method called from TextBoxFor() helper receives default value calculated with
ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model
But SelectInternal() method called from DropDownListFor() helper receives only a name of a control found with ExpressionHelper.GetExpressionText() method.
So SelectInternal() tries to find default value using ViewData.Eval() method from MVC 1. It's known what this method isn't able to extract values from arrays by numeric index.
So in your case are applicable
<%: Html.DropDownListFor(model => model.Sch_ID) %>
<%= Html.TextBoxFor(model => model.Vragen[i].Evvr_Type)%>
but not
<%: Html.DropDownListFor(model => model.Vragen[i].Evvr_Type) %>
because it's equivalent to
<%: Html.DropDownList("Vragen[" + i + "].Evvr_Type") %>
At the same time I want to emphasize again what
<%= Html.TextBoxFor(model => model.Vragen[i].Evvr_Type)%>
isn't equivalent to
<%= Html.TextBox("model.Vragen[" + i + "].Evvr_Type")%>
because latter even in MVC 2 can't bind default value.
Possible workarounds
First. Since SelectInternal() also checks ModelState dictionary you can fill this dictionary before returning the view.
for (int i=0; i < fevb.Vragen.Count(); i++)
ModelState.Add("Vragen[" + i + "].Evvr_Type", new ModelState
{
Value = new ValueProviderResult(fevb.Vragen[i].Evvr_Type, null,
CultureInfo.CurrentCulture)
});
This will be done by MVC itself after from post, so you should do it manually only first time.
Second. Instead of
<%= Html.DropDownListFor(model => model.Vragen[i].Evvr_Type,
ViewData["vraagtype"] as List<SelectListItem>)%>
use
<%= Html.DropDownListFor(model => model.Vragen[i].Evvr_Type,
new SelectList(ViewData["vraagtype"] as IEnumerable, "Value", "Text",
Model.Vragen[i].Evvr_Type))%>
ViewData["vraagtype"] in this case doesn't have to contain objects of SelectListItem, any IEnumerable is enough. You may check SelectList() method description in case of need.

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; }
}

Asp.Net Mvc - RenderAction - Create in a list view

EDIT
I put my solution on a share site. Like that you'll be able to see what I'm talking about. You can download it here : http://www.easy-share.com/1909069597/TestRenderAction.zip
To test it, start the project (let the create form empty) and click the Create button. You'll see what happen.
It's only a example project. I don't care to persist my object to the database for now. I care to make my example work.
I got the following controller :
public class ProductController : Controller
{
public ActionResult List()
{
IList<Product> products = new List<Product>();
products.Add(new Product() { Id = 1, Name = "A", Price = 22.3 });
products.Add(new Product() { Id = 2, Name = "B", Price = 11.4 });
products.Add(new Product() { Id = 3, Name = "C", Price = 26.5 });
products.Add(new Product() { Id = 4, Name = "D", Price = 45.0 });
products.Add(new Product() { Id = 5, Name = "E", Price = 87.79 });
return View(products);
}
public ViewResult Create()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Product product)
{
return View(product);
}
}
The following model :
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
My Product/List.aspx :
<h2>List</h2>
<table>
<tr>
<th></th>
<th>
Id
</th>
<th>
Name
</th>
<th>
Price
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
<%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
</td>
<td>
<%= Html.Encode(item.Id) %>
</td>
<td>
<%= Html.Encode(item.Name) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:F}", item.Price)) %>
</td>
</tr>
<% } %>
</table>
<p>
<% Html.RenderAction("Create"); %>
</p>
My Product/Create.ascx :
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm("Create", "Product", FormMethod.Post)) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Id">Id:</label>
<%= Html.TextBox("Id") %>
<%= Html.ValidationMessage("Id", "*") %>
</p>
<p>
<label for="Name">Name:</label>
<%= Html.TextBox("Name") %>
<%= Html.ValidationMessage("Name", "*") %>
</p>
<p>
<label for="Price">Price:</label>
<%= Html.TextBox("Price") %>
<%= Html.ValidationMessage("Price", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
The problem is that when I hit the create button and I got an error (like empty field), the return view only return my Create.ascx control. It's doesn't return the Product/List.asxp page with in my Create.ascx control with the errors. That's what I would like it's does.
Any idea how I can solve that problem ?
I use Asp.Net Mvc 1 with Asp.Net Futures (which have the Html.RenderAction).
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Product product)
{
...
return View("List");
}
or
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Product product)
{
...
return RedirectToAction("List", "Product");
}
your controller should work like this:
public class ProductController : Controller
{
IList<Product> products;
public ProductController( )
{
products = new List<Product>();
products.Add(new Product() { Id = 1, Name = "A", Price = 22.3 });
products.Add(new Product() { Id = 2, Name = "B", Price = 11.4 });
products.Add(new Product() { Id = 3, Name = "C", Price = 26.5 });
products.Add(new Product() { Id = 4, Name = "D", Price = 45.0 });
products.Add(new Product() { Id = 5, Name = "E", Price = 87.79 });
}
public ActionResult List( )
{
return View(products);
}
public ActionResult Create()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Product product)
{
products.Add(product);
return View("List", products);
}
}
Additionally, you need to call RenderPartial instead of RenderAction, because otherwise your POST method will be invoked (due to you did a post-command by submitting the form):
<p>
<% Html.RenderPartial("Create"); %>
</p>
This should work, you only need to persist your products list, as it will reset with every postback.
I hope this helped you :)
Are the names of your text fields the same as the properties in your Product object?
Does your product object declare it's properties like this;
public string Name {get;set;}
You must have getters and setters on your objects.
EDIT
Wait, are you wanting fields from your list View to be available in the post to your create action? If yes then you need to place the BeginForm at the View level and not the PartialView level.
Only fields contained within the begin form will be posted to your controller.
EDIT 2
Ah, I think I see it now.
In your controller I think you should do a product.IsValid to check first.
But in your html you should also do this;
<%= Html.TextBox("Id", Model.Id) %>
You need to fill in the value for the text box.
Hope this is what you were after.

Resources