views:

116

answers:

1

I am having this issue where I have a form in a partialview but when validating it jumps out of the parent view and appears on its own (having the submit action url).

Also I tried with Render.Action, where validation does not fire at all.

I am using ASP.NET MVC 2 RC and need the server-side validation to work, using the built in validation. Anyone got any suggestions?

Thanks

Code Partial View:

<%=Html.ValidationSummary() %>

<% using (Html.BeginForm("Edit", "Category", FormMethod.Post))
   {%>

    <fieldset>
        <legend>Edit category</legend>
        <p>
            <label for="CategoryName">Category name:</label>
            <%= Html.TextBox("CategoryName", Model.CategoryName)%>
            <%= Html.ValidationMessage("CategoryName", "*")%>
        </p>

        <p class="submit">
            <input type="submit" value="Edit" />
            <%=Html.AntiForgeryToken()%>                
            <%= Html.Hidden("CatId", Model.Id)%>

        </p>
    </fieldset>


<% }

Model Property:

[Required(ErrorMessage="Required")]
public string CategoryName { get; set; }

Edit Action:

[ValidateAntiForgeryToken]
[HttpPost()]
public ActionResult Edit(int catId, CategoryPageViewModel categoryModel)
{
if (ModelState.IsValid)
{
        //TODO
}
return View("list", categoryModel);
}
A: 

Does your main view also display category? You're passing "list" a categoryModel. Is this enough for the main view as well as the partial view.

Richard
I have the form tags inside the PartialView, but not nested.
eb
I've changed my answer in relation to the new info you gave.
Richard
I use /Admin/Category/List to load all categoriesand /Admin/Category/List/Id to load all categories plus a seleted category to edit
eb
Is the list view tightly bound to a collection of Categories? If it is then you're trying to display it while passing it a single category. MVC is probably looking for the closest view that matches this and finding your partial view instead. Make sure that your edit action passes the same model info that the display action does.
Richard