开发者

How can we filter the records in a strongly typed partial view in ASP.NET MVC?

I have a strongly typed partial view that populates all the Records from Search table. Now i have a textbox to enter name & a button to filter the records that can match a name.(Like Search page).

Can anybody provide code sample for this scenario?

My Main Page loads particular view based on the radiobutton selection(Search or Inquiry) as below using JQuery:

/* Loading the partial view based on radio button click... */

        $(document).ready(function() {
            $(':radio').click(function() {
                if (this.value == '2') {开发者_如何学C
                $('#ViewAllInquiries').load('/Home/Inquiry', function(html) { $('#ViewAllInquiries')[0].value = html; });
                }
                else {
                    $('#ViewAllInquiries').load('/Home/Search', function(html) { $('#ViewAllInquiries')[0].value = html; });
                }
            });
        })

Here is my one of the Partial view ControllerCode:

[HttpGet]

    public ActionResult Search()
    {
        var search = from s in entity.Search
                     select s; return PartialView(search);
    }

Here is the User control Partial view(Search.ascx):

>" %>
<table >
<thead>
    <tr>
        <th align="left"> </th>
        <th align="left"> TX_Id</th>
        <th align="left">Name&nbsp;
           <%= Html.TextBox("Name")%>&nbsp;<input type="submit" value="Filter" /></th>
        <th align="left">Email Address</th>
     </tr>

<% foreach (var item in Model) { %> <%= Html.Encode(item.TX_Id) %> "><%= Html.Encode(item.CustomerMaster.FullName()) %> <%= Html.Encode(item.CustomerMaster.MS_Id) %> <% } %>

Thanks for your time.


I do the same thing using an Ajax form. It's really easy. Here's the code I use:

Html:

<div>
    <% 
        using (Ajax.BeginForm("Home", "Search", null,
            new AjaxOptions { UpdateTargetId = "Output" }, 
            new { id = "SearchForm" }))
        {
            %>

                <!-- Form Fields --> 
                <input name="searchField" />
                <input type="submit" value="Search" />   
            <%
        } 
    %>

    <div id="Output">

    </div>

</div>

Then in the controller you just have:

public PartialViewResult Search(FormCollection form)
{

    var model = YourSearchMethod(form["searchField"]);
    return PartialView("Search", model);

}

The div with the id "Output" will be updated with your partial view result every time the submit button is clicked. In your case you have two different potential partial views, just submit the radio button value as part of your form and you can switch the output view from within the controller.

Why use FormCollection instead of parameters? I've had some difficult using named parameters with ajax forms, but you can try it and see how it works. It should look something like this instead:

public PartialViewResult Search(string searchField, bool inquiry)
{
    if (inquiry)
    {
        var model = YourInquiryMethod(searchField);
        return PartialView("Inquiry", model);
    }
    else
    {
        var model = YourSearchMethod(searchField);
        return PartialView("Search", model);
    }
}


I do the same on one of my sites but have implmented it a little diffently.

I have, in my View the following html;

<div class="EditProductContainer hidden"></div>

I also have the following jQuery;

 function editBenefit(objThis) {
     var id = $(objThis).parents('.Benefit').attr("id");
     $.post("/Home/jQueryGetBenefit", { Id: id },
        function(newHTML) {
            $('.EditProductContainer').html(newHTML);
        });
 }

Then in my controller I have;

    [AcceptVerbs(HttpVerbs.Post)]
    public PartialViewResult jQueryGetBenefit(int Id)
    {
        Application application = Helpers.CacheHelper.Get();
        Benefit thisBenefit = application.findBenefit(Id);

        return PartialView("EditBenefit", thisBenefit);
    }

I think this is doing what you want but I'm returning a rendered PartialView and replacing the contents of a containing div with the html generated by the partial view.

Hope this is of some help.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜