开发者

Asp.MVC RenderAction for part of the page doesn't work as I expect

Ok I开发者_StackOverflow中文版 mess my page really bad and I need good help here.

I have a login page with Login and Register partial views.

RegisterView has it's own ViewModel class and it makes me problem.

I render Register partial view by using "RenderAction" helper.

Biggest problem is validation. When I don't enter anything in register fields and click submit register partial view is not updated inside it's parrent LoginView but return me only RegisterView. In other words my validation works but not where I want it to work.

LogOn.cshtml (this one contains register partial view)

 <div id="regstrationForm">
              @{Html.RenderAction("RegisterUser", "Account");}                   
                </div>

Register (Partial View)

@using (@Ajax.BeginForm("RegisterUser", "Account", new AjaxOptions { UpdateTargetId = "reg" }))
{
    <table id="reg">
        ...        
        <tr>
            <td>
                @Html.TextBoxFor(u => u.Username, new { @class = "registrationTextFields", placeholder = "Email" })
            </td>
        </tr>
        <tr>
            <td>
                @Html.TextBoxFor(p => p.Password, new { @class = "registrationTextFields", placeholder = "Password" })
            </td>
        </tr>
        <tr>
            <td align="right">
                <input class="signUpButton" type="submit" value="Sign up" />
            </td>
        </tr>
        <tr>
            <td>

 @Html.ValidationMessage("myerrorsummary")
            </td>
        </tr>
    </table>

This is HttpPost controller method when user click on sign up button.

Here I tried to return "PartialView()" and it turns my input fields to red css style but not display validation information bellow those fields.

[HttpPost]
        public ActionResult RegisterUser(RegisterViewModel logOnViewModel)
        {
            if (ModelState.IsValid)
            {
                MembershipCreateStatus result;

                try
                {
                    ...
            }
            else
            {                
                ModelState.AddModelError("myerrorsummary", "The input is not valid");

                ViewBag.CountryList = countryRepository.GetAllCountries();
                ViewBag.CityList = cityRepository.GetAllCitiesByCountryId(1);

                return View(logOnViewModel);
            }
        }

Asp.MVC RenderAction for part of the page doesn't work as I expect


I suspect that you forgot to include the following script in your main view:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

This is what makes Html helpers such as Ajax.BeginForm and Ajax.ActionLink to perform AJAX requests instead of normal.

You might also take a look at the following article which explains in more details about how those helpers work in ASP.NET MVC 3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜