简体   繁体   中英

How to get Single Object in Controller from List of Objects using Razor View in ASP.NET MVC

This is my Controller:

namespace OBBMS.Controllers
{
    public class BDMController : Controller
    {
        public ActionResult Index()
        {   
            OBBMS.Models.User objUser = new Models.User();
            objUser.lstUser = DB_Interactions.BDMGetUsers("Pending");
            return View(objUser);
        }

        [HttpPost]
        public ActionResult Index(OBBMS.Models.User objUser)
        {
            return View();
        }
    }
}

Here is View Code:

@model OBBMS.Models.User
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutBloodDonationManagement.cshtml";
}
@using (Html.BeginForm("Index", "BDM", FormMethod.Post, new {id = "BDM"}))
{
<table id="mytable" class="table table-bordred table-striped">
<thead>
    <tr>
        @*<th>Post ID</th>*@
        <th>Full Name</th>
        <th>Blood Group</th>
        <th>Email Address</th>
        <th>Contact No</th>
        <th>Address</th>
        <th>Post Title</th>
    </tr>
</thead>
<tbody>
    @foreach(var obj in Model.lstUser)
    {
        <tr>
            <td style="display:none!important;">@obj.PostID</td>
            <td>@obj.FullName</td>
            <td>@obj.BloodGroupName</td>
            <td>@obj.EmailAddress</td>
            <td>@obj.ContactNo</td>
            <td>@obj.Address</td>
            <td>@obj.PostTitle</td>
            <td><input type="submit" value="Approve" class="btn btn-primary btn-xs" data-title="Approved" data-toggle="modal" data-target="#edit" /></td>
        </tr>
    }
</tbody></table>}

I have a submit button at the end of each row, I need to get Single instance of User object instead of complete list in HttpPost ActionResult Method when a specific row button is click. I just need to get the object only the row button is clicked.

If possible: I do not want to use any JavaScript/jQuery/Ajax, everything should be done in APS.NET MVC framework.

If not possible: Suggest me best and easiest way to do it.

One possible solution is to use the HTML 5 form attribute . The attribute can be used to indicate which form an element belongs to. For example:

  @foreach(var obj in Model.lstUser) { <tr> <td><form id="@("form"+obj.PostID)"><input type="hidden" name="id" value="@obj.PostID" /></form></td> <td><input form="@("form"+obj.PostID)" type="text" name="FullName" value="@obj.FullName" /></td> <td><input form="@("form"+obj.PostID)" type="text" name="BloodGroupName" value="@obj.BloodGroupName" /></td> <-- more td elements here --> <td><input form="@("form"+obj.PostID)" type="submit" value="Approve" class="btn btn-primary btn-xs" data-title="Approved" data-toggle="modal" data-target="#edit" /></td> </tr> } 

Check here for a list of supported browsers.

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