简体   繁体   中英

How to keep click event dynamically created buttons in mvc

i have retrive name of buttons from database those button when i click a button i need to show the some report.now i have everything is done.i need to know how keep unique id for those buttons and how they worked when i click button i need to show report in a separate page...

  [HttpGet]
        public ActionResult Estate()
        {

            return View("Estate", new EstateFunc().Estates());

        }

        [HttpPost]
        public ActionResult Estate(int id=0)
        {
            if(id>0)
            {

            }
            return View();

         }

With MVC

View:

@foreach (var item in Model)
{
    <tr>
        <td>@item.Something...</td>
        <td>@item.AnotherThing</td>
        <td>
<a href="@Url.Action("MyAction", "MyController", new { Id = item.Id })" class="btn"></a></td>
    </tr>
}

This Razor code will generate as many button as the count of the collection items in your model. Every item in the collection should have unique ID and as you see we put this ID as parameter. So when the user clicks on any of the buttons, the unique ID will tell us which button was clicked (and to which item we should refer to)

Controller:

public ActionResult MyAction(int id = 0)
        {
            var model = ReportViewModel(id);

            return ReportView(model);
        }

private ReportViewModel(int id)
{
   // Get Report from Database Where Id = id
   // Populate ViewModel from the database data
   // Return reportViewModel;
}

EDIT (adding example based on your code)

   @foreach (var item in Model)
   {
    <div class="floated">

    <a href="@Url.Action("MyAction", "MyController", new { Id = item.Id })" 
       class="Estates_button">@item.EstateName</a>

    </div>
    }

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