将字符串值传递给控制器​​按钮单击MVC2

时间:2014-05-23 11:21:49

标签: asp.net-mvc-2

我试图在按钮点击时向控制器发送一些值 这是视图

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Customers</h2>
    <table>
    <tr style=" background-color:White; font-style:normal; font-size:x-large;">
   <td>S#</td> <td>Name</td> <td>Email</td> <td>Phone</td> <td>Edit</td>
    <%int i = 1;
      string name, id, phone, email;
       %>
    </tr>
    <% foreach (var clientsModel in ViewData["AllClients"] as List<MvcApplication5.Models.ClientsModels>){ %>
    <tr style=" background-color:black; font-style:oblique; color:Wheat; font-size:large;">
    <td><%:i %></td>
    <%     name = clientsModel.Name;
           phone = clientsModel.Phone;
           id = clientsModel.UserID;
           email = clientsModel.Email; %>
        <td>
    <label  id="Name"><%= clientsModel.Name %></label>        
    </td>
        <td>
    <label  id="Email"><%= clientsModel.Email %></label>        
    </td>
        <td>
    <label id="Phone"><%= clientsModel.Phone %></label>        
    </td>
    <td><button id="edit" >Edit</button></td>
    </tr>           

<%   i++;


       } %>
</table>

</asp:Content>

这里是我想要发送值的控制器

public ActionResult Edit(string name, string id, string phone, string email)
{
    ViewData["name"] = name;
    ViewData["phone"] = phone;
    ViewData["id"] = id;
    ViewData["email"] = email;
    return View();
}

我想将名称,电话,电子邮件和身份证的值发送给控制器......

1 个答案:

答案 0 :(得分:0)

如果您使用的是Ajax

    var dataPost = { 'id': id, 'val': val };
$.ajax({
    type: 'POST',
    url:  '/Controller/Action',
    data: JSON.stringify(dataPost),
    contentType: 'application/json',
    success: function () {
        alert("succes");
    },
    error: function () {
        alert("error");
    }
});