Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 使用foreach循环增加变量_Asp.net Mvc 3_Razor_For Loop_Foreach - Fatal编程技术网

Asp.net mvc 3 使用foreach循环增加变量

Asp.net mvc 3 使用foreach循环增加变量,asp.net-mvc-3,razor,for-loop,foreach,Asp.net Mvc 3,Razor,For Loop,Foreach,我试图在下面循环这些变量(项目名称、数量和金额) PayPal.Model public class PayPal { public string cmd { get; set; } public string business { get; set; } public string no_shipping { get; set; } public string @return { get; set; } public string can

我试图在下面循环这些变量(项目名称、数量和金额)

PayPal.Model

    public class PayPal
    {
    public string cmd { get; set; }
    public string business { get; set; }
    public string no_shipping { get; set; }
    public string @return { get; set; }
    public string cancel_return { get; set; }
    public string notify_url { get; set; }
    public string currency_code { get; set; }
    public string item_name { get; set; }
    public string quantity { get; set; }
    public string amount { get; set; }
}
和PostToPayPal.cshtml

    <form id="frm" action="@ViewBag.actionURL">
        @Html.HiddenFor(model => model.cmd)
        @Html.HiddenFor(model => model.business)
        @Html.HiddenFor(model => model.no_shipping)
        @Html.HiddenFor(model => model.@return)
        @Html.HiddenFor(model => model.cancel_return)
        @Html.HiddenFor(model => model.notify_url)
        @Html.HiddenFor(model => model.currency_code)
        @Html.HiddenFor(model => model.item_name)
        @Html.HiddenFor(model => model.quantity)
        @Html.HiddenFor(model => model.amount)
    </form>

@Html.HiddenFor(model=>model.cmd)
@Html.HiddenFor(model=>model.business)
@Html.HiddenFor(model=>model.no_shipping)
@Html.HiddenFor(model=>model.@return)
@Html.HiddenFor(model=>model.cancel\u返回)
@Html.HiddenFor(model=>model.notify\uURL)
@Html.HiddenFor(model=>model.currency\u代码)
@Html.HiddenFor(model=>model.item_name)
@Html.HiddenFor(model=>model.quantity)
@Html.HiddenFor(model=>model.amount)

我引用了

中的PayPal方法,我认为您可能需要的是:

@using (Html.BeginForm("PostToPaypal", "ShoppingCart"))
{

    for (int i=0; i<Model.CartItems.Count; i++)
    {
        @Html.HiddenFor(m => m.CartItems[i].Product.Title)
        @Html.HiddenFor(m => m.CartItems[i].Count)
        @Html.HiddenFor(m => m.CartItems[i].Product.Price) 
    }

<input type="submit" name="btnsubmit" value="Pay with PayPal" />

我试过你的方法。没有返回错误。但当我向PayPal结账时,没有传递任何值。你能更具体地说我应该在控制器里做什么吗?谢谢@Mohamed Azlan-你能在你的问题中发布你正在使用的模型和控制器的操作吗?这样我就可以更好地理解问题可能是什么。谢谢。我已经通过编辑帖子添加了控制器、模型和Cshtml页面。感谢您对此进行研究。@Mohammed Azlan-您在表单中显示了一组项,但您只是试图绑定到操作中的单个项,这就是为什么没有绑定任何项。这就是为什么您需要绑定到某种类型的集合。我想你应该用
PostToPaypal(cartimes模型)
代替
PostToPaypal(string item\u name,string quantity,string amount)
。替换为(cartimes模型)后,我得到了这个错误;找不到类型或命名空间名称“CartItems”(是否缺少using指令或程序集引用?)
    <form id="frm" action="@ViewBag.actionURL">
        @Html.HiddenFor(model => model.cmd)
        @Html.HiddenFor(model => model.business)
        @Html.HiddenFor(model => model.no_shipping)
        @Html.HiddenFor(model => model.@return)
        @Html.HiddenFor(model => model.cancel_return)
        @Html.HiddenFor(model => model.notify_url)
        @Html.HiddenFor(model => model.currency_code)
        @Html.HiddenFor(model => model.item_name)
        @Html.HiddenFor(model => model.quantity)
        @Html.HiddenFor(model => model.amount)
    </form>
@using (Html.BeginForm("PostToPaypal", "ShoppingCart"))
{

    for (int i=0; i<Model.CartItems.Count; i++)
    {
        @Html.HiddenFor(m => m.CartItems[i].Product.Title)
        @Html.HiddenFor(m => m.CartItems[i].Count)
        @Html.HiddenFor(m => m.CartItems[i].Product.Price) 
    }

<input type="submit" name="btnsubmit" value="Pay with PayPal" />
    [HttpPost]
    public ActionResult PostToPaypal(MODELTYPEHERE[] model)
    {
        //processing here
    }