Подтвердить что ты не робот

Кнопка MVC Razor позволяет даже пропустить параметр

Я новичок в MVC Razor.

У меня есть это представление:

@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>
}

Какой вызов вызывает этот метод:

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

Что, в свою очередь, вызывает этот частичный вид:

@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 />

Которая заканчивает отображение этого:

enter image description here

Это работает до этого момента.

То, что я хочу, - это нажать кнопку рядом с именем клиента и поднять учетную запись клиента.

Как мне привязать этого клиента к кнопке, чтобы узнать, кто подтягивается и как кнопка нажимает на нее?

4b9b3361

Ответ 1

Вам нужно передать номер клиента после нажатия кнопки:

Если у вас есть номер клиента как свойство в Модели, вы можете сделать что-то вроде:

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

Вы могли бы POST использовать эти данные на сервере, используя @Html.ActionLink, @Ajax.ActionLink или jQuery:

Действие Ссылка

@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) { 

       } 
 }); 

Ответ 2

Я думаю, это будет трюк! oid будет идентификатором клиента (я не думаю, что путь в порядке:))

@Ajax.RawActionLink("Action", "Controller", new { oid = '@Model.customerID'}, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "the view you want to show" }, new { id = "btnNewB", @class = "your btn class" })

удачи;)