Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Razor button click even pass parameter with it

I'm new to MVC Razor.

I have this view:

@model SuburbanCustPortal.Models.CustomerModel

@{
    ViewBag.Title = "Customer Summary";
}

<h2>Customer Summary Screen</h2>
<p>
    Please select an account below or add an existing account.
</p>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
  @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")

  <div>
    <fieldset>
      <legend>Existing Accounts</legend>

      @Html.Action("ExistingAccounts2")

      <p>
        <input type="submit" value="Add an Account" />
      </p>


    </fieldset>
  </div>
}

Which calls this method:

[Authorize]
public ActionResult ExistingAccounts2()
{
  return PartialView("ExistingAccounts", _client.RequestCustomersForAccount(User.Identity.Name));
}

Which in turn calls this partial view:

@model IEnumerable<SuburbanCustPortal.SuburbanService.CustomerData >

<br />
<br />
<table>

  @if (Model != null)
  {
    foreach (var usr in Model)
    {
      <tr>      
        <td>
          <input id="btnShowCustomer" name="btnShowCustomer2" type="submit" value="View"/>           
        </td>
        <td>
          @usr.AccountId
        </td>
        <td>
          @usr.Name
        </td>
@*        <td>
          @usr.DeliveryStreet
        </td>*@
      </tr>
    }
  }

</table>
<br />

Which ends up displaying this:

enter image description here

This works up to this point.

What I want to so is be able to click on the button next to the customer's name and it pull up the customer's account.

How do I tie that customer to the button to know who to pull up and how do have the button click pull it up?

like image 974
ErocM Avatar asked Sep 07 '12 13:09

ErocM


People also ask

How pass textbox value to controller in MVC on button click?

MVC will automatically take the value of UnboundTextBoxName and insert that value into the parameter of the same name. Show activity on this post. You should be using <form> tags and set the action to your controller and have set a place in your model to store the data.

How can call POST action method on button click in MVC?

click(function () { //On click of your button var property1 = $('#property1Id'). val(); //Get the values from the page you want to post var property2 = $('#property2Id').

Can you mix MVC and Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder.


1 Answers

You need to pass the Customer Number back once the button is clicked:

If you have the customer number as a property in the Model you could do something like:

<input id="btnShowCustomer" data-customerNumber="@usr.CustomerNumber" />

You could then POST this data to the Server using an @Html.ActionLink, @Ajax.ActionLink, or jQuery:

Action Link

@Html.ActionLink("LoadInfo", "Info", new {[email protected]})

jQuery

$("#btnShowCustomer").click(function() {

 var customerId = $("#btnShowCustomer").attr("data-customerNumber");
  $.ajax({ 
       type: "POST", 
       data: "customerId=" + customerId, 
       url: '@Url.Action("MyAction", "MyController")', 
       success: function (result) { 

       } 
 }); 
like image 103
Darren Avatar answered Nov 08 '22 21:11

Darren