当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - @URL.Action on button click to load partial view

问题描述:

In my form I have email field , search Button and place holder for the partial view

@using (Html.BeginForm())

{

Html.TextBoxFor(model => model.emailAddress, new { style = "width:400px" , @class = "emailValue"})

@Html.ValidationMessageFor(model => model.emailAddress)

<button name="button" value="search" id="SearchResultbtn">

Search</button>

<div id="searchResult">

</div>

}

The search result is displayed in partial view by using jquery

$(document).ready(function (e) {

$("#SearchResultbtn").click(function (e) {

e.preventDefault();

$("#searchResult").load('@Url.Action("AdminSearchResult", "Administration")');

});

});

In my controller I have a main Action method

[HttpPost]

public ActionResult CreateAdmin(string button, Administration admin)

{

}

and Action method that handles partial view

public ActionResult AdminSearchResult(string sortOrder, int? page)

{

var result = Administration.GetAdministrationList();

return PartialView("AdminSearchResult", result);

}

Now what I want is to first validate email on button click if email is valid then load the partial view if not throw the error message.

网友答案:

You again, @user2225604! You don't need any script or anything fancy at all to do this. You already have unobtrusive js enabled as per my previous answer, so...

From the behaviour you mentioned, I'm thinking you simply need something like this:

@using (Ajax.BeginForm("AdminSearchResult", "ControllerName", null, new AjaxOptions { UpdateTargetId = "searchResult", InsertionMode = InsertionMode.Replace }))

instead of:

@using (Html.BeginForm())

Then you can get rid of the script entirely. You might have to pass a routeValue for that sortOrder parameter in your Ajax.BeginForm, but other than that, you should be good to go.

分享给朋友:
您可能感兴趣的文章:
随机阅读: