dimanche 29 janvier 2017

ASP MVC 5 - keep @html.checkbox state after page reload after clicking "submit button"

I created a page that I can search the store detail via entity framework I added a column of checkbox in the table. I would like to keep the checkbox "checked=True" after I submit via the search button.

What would be the recommend way to achieve that ?

I tried following method, but the checkbox get "unchecked" after I click submit 1. http://ift.tt/25ms3xQ

View as following :

@using (Html.BeginForm())
{
<p>
    Find by name: @Html.TextBox("SearchString") 
    <input type="submit"  name ="StoreIndexButton" value="Search" />
</p>
}
<table class="table" id="displayresult">

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.sname)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.smarket)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.sstatus)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.soper)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.sowneroperator)
    </td>
    <td>
        @Html.ActionLink("Details", "Details", new { id=item.store1 })
    </td>
    <td>
        @Html.CheckBox("selected",new { value = item.store1,     id="selected"+item.store1.ToString() })
    </td>
</tr>
}

</table>

Controller as following :

public ActionResult Index(string StoreIndexButton,string searchString)
    {
        var AR_stores = (from m in db.stores
                         select m).Take(10);
        string[] selectedList = Request.Form.GetValues("selected");
        if (!String.IsNullOrEmpty(searchString) && StoreIndexButton =="Search")
            {
                    AR_stores = (from m in db.stores
                                 select m).Where(s =>   s.sname.Contains(searchString));
                }
            return View(AR_stores);    
    }

Model as following :

 using System;
using System.Collections.Generic;

public partial class store
{
    public int store1 { get; set; }
    public string sname { get; set; }
    public string smarket { get; set; }
    public string sstatus { get; set; }
    public string soper { get; set; }
    public string sowneroperator { get; set; } 
}




Aucun commentaire:

Enregistrer un commentaire