简体   繁体   中英

How do I submit only a part of a (large) form in ASP.NET MVC?

I have a model which is a list of objects which I am rendering out to a page inside a form. The user selects the objects with the checkbox, hits submit, the model is posted back and I perform some processing on the selected items.

@using (Html.BeginForm("Method", "Controller"))
{
<table id="mytable">
    <tr>
        <th>Some Properties</td>
        <th>Selected</td>
    </tr>

@foreach (var item in Model)
    <tr>
        @Html.HiddenFor(m => item.Id)
        <td>@item.Id</td>
        <td>@Html.CheckBoxFor(m => item.Selected)</td>
    </tr>
</table>
<input id="btnGo" type="submit" name="submitButton" value="Go"/>        
}

(That is basically it. I don't actually think the above works - I had to seperate it out so the 'foreach' part is in a partial view - but I'm sure you get the idea).

Usually there aren't many rows, but sometimes there can be a lot.

When it does get very high (2000 plus), even if the user only ticks 1 box, it can take up to 10 minutes to post back the entire model, when 99.9% of it is not needed.

How would I go about only submitting the relevent (checked) items from the model?

The only way I could get it to work only submitting the checked items was with some javascript to force it into the url.

$(document).ready(function () {
    $('#btnGo').click(function () {
        var idArray = new Array();
        $("#mytable input:checkbox:checked").each(function () {
            idArray.push(this.id);
        });
        window.location = "/My.Web.Page/Controller/Method?selectedIds=" + idArray;
    });
});

But I don't like it because i'm having to hardcode the url, and the user can just hit 'refresh' on the browser to then basically reprocess everything.

Well, using only html you can't do partial postbacks. If you need to be able to post back only some data, you should use Ajax.

Btw, 10min processing on 2000 items? What are you doing with the data?!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM