简体   繁体   中英

Asp.Net MVC how to call a method from a instanc of a class by buttonClick event

I made a WakeUp program for my Company and I'm new at ASP.NET. I´ve got the host class with the strings dnsName , IpAddress , MacAddress and the method WakeUP() . Now I need a button, which calls the WakeUP method of the instance of Model[i], by the onButtonClick Event.

@for (int i = 0; i < Model.Length; i++)
{
    <tr>
        <td>@Model[i].DnsName</td>
        <td>@Model[i].IpAddress</td>
        <td>@Model[i].MacAddress</td>
        <td>
           --> Here I need a button to call the wakeUp() method from Model[i]
        </td>
    </tr>
}

thanks for help :)

Let's call your model Server , and from the looks of it the view has an array of Server (or a List , IEnumerable etc...). The code below will add an anchor, which will make a GET to a route and pass the Id of the Server , assuming that you got an Id . If not, you can pass something else that is unique for the specific Server . This is very handy because you don't need to send the entire model, you simply pass the Id and let the Controller handle that part instead. The Controller can fetch the correct Server instance based on the Id , and then wake it up.

View:

@model Server[]

<! -- Other stuff -->
<table>
@for (int i = 0; i < Model.Length; i++)
{
    <tr>
        <td>@Model[i].DnsName</td>
        <td>@Model[i].IpAddress</td>
        <td>@Model[i].MacAddress</td>
        <td>
            @Html.ActionLink(string.Format("Wake up {0}", Model[i].DnsName), "WakeUp", "Home", new {id = @Model[i].Id})
        </td>
    </tr>
}
</table>

And your controller:

public class HomeController : Controller
{
    [HttpGet]
    public ActionResult WakeUp(int id)
    {
        // id will be the id of the server.
        // Do your logic here.

       return View(); // Or whatever you want to return.
    }
}

If you really need to pass the entire model, you should do a post instead. But I don't think you need to, since the model seems to be server-generated and thus not necessary to send back. There's no user input there...

And lastly, you shouldn't have business logic in your model. So the method you're calling WakeUP() should not be in the Server -object. I think it's better if you do it in a controller, manager or somewhere else, and pass the Id to that method. Something like this:

public class HomeController : Controller
{    
    [HttpGet]
    public ActionResult WakeUp(int id)
    {
       // Possibly some validation here?

       // Send the id to the manager.
       var manager = new WakeUpManager();
       manager.WakeUp(id);

       return View(); // Or whatever you want to return.
    }
}

public class WakeUpManager
{
    public void WakeUp(int serverId)
    {
        // Wake the server up... or snooze... or whatever. :)
    }
}

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