为什么控制器中的参数不能在定义内完全使用?

时间:2019-07-06 15:53:46

标签: c# asp.net-mvc-4

控制器中的参数是一个视图模型,其中包含另外两个类的声明。在调用action方法时,该参数具有值,但是在定义内部使用时,只有一个类变量返回值。 该如何解决?

View.cshtml

@using(Html.BeginForm("makepayment","home",FormMethod.Post))
{
    if (Model != null) {
        //for (var i = 0; i < Model.Count();i++ )
        for (var i = 0; i < Model.Count; i++) {
            var index = i;
            @Html.HiddenFor(m => Model[index].cart.vid)
        }

        @Html.TextBoxFor(m => m[0].payment.cname);
        @Html.TextBoxFor(m => m[0].payment.number);
        @Html.TextBoxFor(m => m[0].payment.securitycode);
        @Html.TextBoxFor(m => m[0].payment.expdate);  //foreach(var item in Model)
    }                           

    <input type="submit" value="pay" />
}

控制器

[HttpPost]
public ActionResult makepayment(List<paymentcart> c)
{
    int status;
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        using (SqlCommand cmd = new SqlCommand("makepayment", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (var abc in c)//here abc has values of class cart.cs and another one is null
            {
                cmd.Parameters.AddWithValue("@vid", abc.cart.vid);
                cmd.Parameters.AddWithValue("@number", abc.payment.number);
                cmd.Parameters.AddWithValue("@cname", abc.payment.cname);
                cmd.Parameters.AddWithValue("@cvc", abc.payment.securitycode);
                cmd.Parameters.AddWithValue("@exdate", abc.payment.expdate);
                //cmd.Parameters.AddWithValue("@amt", abc.cart.totalamount);
            }


            con.Open();
            status = cmd.ExecuteNonQuery();
        }
    }

     //It only returns cart.cs class values and payment class remains null
     return RedirectToAction("ViewCart");
}

错误是这样的 Error look like this in editor

0 个答案:

没有答案