Hi, i have a very strange problem, i have a main controller with it's own model, and in the Create new Record View, inside the Form i call RenderAction to render an ASCX View with it is own Dedicated Model and controller, the problem is that that when i submit the form the validation is only taking place on the Main Model and it is not happening on the Model that belongs to the ASCX View, can any one tell me what am i doing wrong over here ?
thanks in advanced. I am Sorry not to post a sample here it is
Parent Controller
[HttpPost]
public ActionResult Create(Parent p, FormCollection collection)
{
ViewData["fk"] = GenerateId();
DataClasses1DataContext DB = new DataClasses1DataContext();
if (!ModelState.IsValid)
return View();
try
{
Parent Pr = p;
DB.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View(p);
}
}
Parent View
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Parent>" %>
Create
Create
<%:ViewData["fk"] %> <% using (Html.BeginForm()) {%> <%: Html.ValidationSummary(true) %> Fields <%: Html.LabelFor(model => model.Name ) %> </div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Name)%>
<%: Html.ValidationMessageFor(model => model.Name)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Family)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Family)%>
<%: Html.ValidationMessageFor(model =>model.Family)%>
</div>
</fieldset>
<% Html.RenderAction("Create","Child", new { Value = ViewData["fk"]}); %>
<p>
<input type="submit" value="Create" />
</p>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
Child Controller
[HttpPost]
public ActionResult Create(Children c, FormCollection collection, String fk)
{
var DB = new DataClasses1DataContext();
ValidateModel(ViewData.ModelState);
ViewData["fk"] = fk;
if (!ModelState.IsValid)
{
return View("ChildCreate");
}
try
{
Children Ch = c;
DB.SubmitChanges();
return View("ChildCreate");
}
catch
{
return View("ChildCreate", c);
}
}
Child View
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication1.Models.Children>" %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.ParentID ) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ParentID, new { Value = ViewData["fk"] })%>
<%: Html.ValidationMessageFor(model => model.ParentID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Rank) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Rank) %>
<%: Html.ValidationMessageFor(model => model.Rank) %>
</div>
</fieldset>
Validation Class for Child
namespace MvcApplication1.Models
{
[MetadataType(typeof (CValidation))]
public partial class Child
{
}
public class CValidation
{
[Required(ErrorMessage = "Parent is required.")]
public String ParentID { get; set; }
[Required(ErrorMessage = "Rank is required.")]
public String Rank { get; set; }
}
}