多个提交按钮。 [英] multiple submit button.

查看:69
本文介绍了多个提交按钮。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发MVC3中的购物车网站,使用razor.below代码是查看代码,其中我展示了多个产品,每个产品都有数量文本框和一个提交按钮。所以,在控制器中我怎么知道哪个提交按钮用户调用以及如何从文本框中读取数量,因为所有提交按钮和文本框都具有相同的ID,因为它们是动态创建的。







 @ using(Ajax.BeginForm(  addtocart   Chocolatier,FormMethod.Post, null ))
{

< table>
< tr>

@foreach(System.Data.DataRow i in Model.Rows)
{
< tr>< ; TD> < img src = @ Url.Content(〜/ Content / abc.gif alt = Imge height = 40 width = 40 /> < / td > < / tr >
foreach (System.Data。 DataColumn j in Model.Columns)
{
< tr> < td style = width:200px>< h5> @ j.ToString() :< / h5 > @我[j] < / td >
< / tr >
}

< td>输入数量:< input id = txtqty type = text style = width:50px /> < / td >
< tr>< td>< input type = submit value = 添加到购物车 /> < / < span class =code-leadattribute> td > < / tr >

< tr> < td> __________________________________________________________________________ < / td > < / tr >


}

< / tr >
< / >

解决方案

首先,拥有相同ID的html元素是非常糟糕的,根本不被接受!!!

在您的情况下,您可以通过轻松附加行索引......也可以使用提交按钮完成...现在当你有一个提交点击时,你可以获取控件的id导致表单提交,这将为您提供行索引。基于此,您可以轻松获得相应的文本框...



此方案可以有多种解决方案。但是请在任何情况下都避免重复ID。


你需要更改代码的逻辑。



使用相同的代码

请记住解决方案1的答案



您可以按下按钮查看



 <  输入   类型  = 提交   名称    =  action        value   = 添加到购物车(产品名称) < span class =code-attribute>       /  >  







和在控制器中您可以捕获按钮的名称



  string  StrAcctinName =请求[  action]。ToString(); 





并使用case语句或if语句,你可以称之为代码块。



但是以上不是完美的解决方案。

所以你可以这样做



< script type = < span class =code-string>  text / javascript> 
function 保存(id,qty){


.ajax({
type: POST
contentType: application / json; charset = utf-8
data: {id:' + id + ',qty:' + qty + '}
url:' @ Url.Action(addtocart,Chocolatier)'
dataType: json
async: true

成功: function (req){
消息或scrpt
},
错误: function (req,status,error){
alert(req.d);
alert(status);
alert(错误);
}

});


}
< / script>





 <  表格 >  
< tr >

@foreach(Model.Data中的System.Data.DataRow)
{
< tr > < td > < / td > < / tr >
foreach(Model.Columns中的System.Data.DataColumn j)
{
< tr > < td style = width:200px > < h5 > @ j.ToString():< / h5 > @i [j] < / td >
< / tr >
}

< td > 输入数量:< 输入 id = txtqty 类型 = text 样式 = width:50px / > < / td >
< span class =code-keyword><
tr > < td > < span class =code-keyword>< input type = button önclick = 保存( + j.id + < span class =code-attribute>, + j.qty value = 添加到购物车 > < / input > < / td > < ; / tr >

< tr > < td > ________________________ __________________________________________________ < / td > < span class =code-keyword><
/ tr >


}

< / tr >
< / table >







您也可以查看以下链接.....



http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-8 [ ^


i am developing shopping cart website in MVC3 with razor.below code is view code,in which i am showing multiple products,each product has quantity text box and one submit button.So,in controller how would i know which submit button is called by user and how to read quantity from textbox,because all submit button and textbox has same id because they are created dynamically.



@using (Ajax.BeginForm("addtocart", "Chocolatier",FormMethod.Post,null))
 {

<table>
    <tr>

   @foreach (System.Data.DataRow i in Model.Rows )
   {
       <tr><td>  <img src=" @Url.Content("~/Content/abc.gif") "  alt="Imge" height ="40" width ="40"/></td></tr>
          foreach (System.Data.DataColumn j in Model.Columns)
          {
             <tr> <td style="width : 200px"><h5>@j.ToString(): </h5>@i[j]</td>
             </tr>
          }

           <td>Enter quantity : <input id="txtqty" type ="text" style="width : 50px" /></td>
     <tr><td><input type ="submit" value="Add to cart" /></td></tr>

       <tr>     <td>__________________________________________________________________________</td></tr>


                   }

    </tr>
</table>

解决方案

First of all it is very bad to have html elements with same ID, simply not accepted!!!
In your case, you can make the id distinct by easily appending the row index... and same can be done with submit button... Now when you have a submit click you can take the id of control causing form submit, which will give you the row index. Based on this you can easily get the corresponding text box...

There can be multiple solutions to this scenario. But please avoid duplicate ID's in any case.


You need to change logic of the code hare.

With The same code
And Keep in mind answer of Solution 1 also

You can put the button on view as follows

<input type="submit" name = "action"   value="Add to cart (Product Name)"   />




and In the controller You can catch the name of the button

string StrAcctinName = Request["action"].ToString();



and by using case statement or if statement you can call you code block.

But above is not perfect solution.
so you can do as follows

<script type="text/javascript">
    function Save(id , qty) {


.ajax({ type: "POST", contentType: "application/json; charset=utf-8", data: "{id:'" + id + "',qty:'" + qty + "'}", url: '@Url.Action("addtocart", "Chocolatier")', dataType: "json", async: true, success: function (req) { message or scrpt }, error: function (req, status, error) { alert(req.d); alert(status); alert(error); } }); } </script>



<table>
    <tr>

   @foreach (System.Data.DataRow i in Model.Rows )
   {
       <tr><td>  </td></tr>
          foreach (System.Data.DataColumn j in Model.Columns)
          {
             <tr> <td style="width : 200px"><h5>@j.ToString(): </h5>@i[j]</td>
             </tr>
          }

           <td>Enter quantity : <input id="txtqty" type="text" style="width : 50px" /></td>
     <tr><td> <input type="button"  önclick="Save(" + j.id + " ," + j.qty ")" value="Add to cart"> </input></td></tr>

       <tr>     <td>__________________________________________________________________________</td></tr>


                   }

    </tr>
</table>




You can check following link also .....

http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-8[^]


这篇关于多个提交按钮。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆