IList没有正确回发到控制器

时间:2015-03-06 03:35:37

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

我遇到了需要批量编辑并将记录发回我的mvc 4控制器的情况。我有一个自定义视图模型的列表,我可以在索引视图上正确传递和编辑。但是,当我提交表单并尝试访问post方法中的列表时,我无法检索相同的列表。最多,我只能得到第一条记录,或者我得到空。如何获取整个对象列表?

视图模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace SitefinityWebApp.Mvc.Models
{
    public class CheckoutViewModel
    {
        public string Title { get; set; }
    [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        [Required]
        public string Email { get; set; }
        [Required]
        public string Phone { get; set; }
        public string PTIN { get; set; }
        public string Sku { get; set; }
        public Guid CartOrderId { get; set; }
        public string IRSCourseNumber { get; set; }
        public string Id { get; set; }

    }
}

控制器:

public ActionResult Index()
    {
        Guid gdShoppingCartId = cmmObj.GetShoppingCartId();
        List<SitefinityWebApp.Custom.Surgent.sf_ec_cart_detail> cartdetails = new List<SitefinityWebApp.Custom.Surgent.sf_ec_cart_detail>();
        SubSiteProductRepository ssp = new SubSiteProductRepository();
        List<CheckoutViewModel> cvml = new List<CheckoutViewModel>();

        if (gdShoppingCartId != Guid.Empty && gdShoppingCartId != null)
        {
            cartdetails = DbContext.sf_ec_cart_details.Where(d => d.cart_order_id == gdShoppingCartId).ToList();
            if (cartdetails.Count > 0)
            {
                if (cartdetails.Where(c => c.need_registration2 == "true").Count() > 0)
                {
                    var webinars = LoadWebinar();
                    //WebinarViewModel wvm = new WebinarViewModel();
                    foreach (DataRow row in webinars.Rows)
                    {
                        cvml.Add(new CheckoutViewModel
                        {
                            Sku = row["sku"].ToString(),
                            Id = row["id"].ToString(),
                            Title = row["title_"].ToString(),
                            CartOrderId = gdShoppingCartId,
                            IRSCourseNumber = row["irsCourseNumber"].ToString()
                        });
                        //foreach (DataColumn column in table.Columns)
                        //{
                        //    object item = row[column];
                        //    // read column and item
                        //}
                    }
                }


            }
        }
        return View(cvml);
    }

    [HttpPost]
    public ActionResult Index([Bind(Prefix="cvm.")]IList<CheckoutViewModel> cvm)
    {
        int viewmodelerror = 0;
        foreach(CheckoutViewModel c in cvm){
            if (ModelState.IsValid)
            {
                //if (c.PTIN != null || c.PTIN != "")
                    //return Redirect("~/CheckOut");
            }
            else
            {
                viewmodelerror++;
                //return View(cvm);
            }
        }

        return View(cvm);
            //return new EmptyResult();
    }

索引视图:

    @model IList<SitefinityWebApp.Mvc.Models.CheckoutViewModel>
@{
    var allWebinars = "http://" + Request.Url.Authority + "/CheckOut";
}



<div id="registration-content">
    <div class='row'>
        <div class='columns medium-12'>
            <h1>Attendee Information</h1>
        </div>
    </div>
    <div class='row'>
        <div class='columns medium-9'>
            <div class='columns medium-12'>
                <p class='instructions'>Please complete attendee information for the following courses:</p>
            </div>

            @foreach (SitefinityWebApp.Mvc.Models.CheckoutViewModel cvm in Model)
            {
                @Html.EditorFor(x => cvm.FirstName)
            }
            @Html.BeginForm(){
               @for (int i = 0; i < Model.Count; i++)
               {
                   <div class='columns medium-12'>
                       <!--FIRST PRODUCT DESCRIPTION-->
                       <div class="product-description">
                           <p class='course-link'><a href="webinars.html">@Html.DisplayFor(t => t[i].Title)</a></p>
                           <p class='small'>Accounting</p>
                       </div>
                   </div>
                   <div class='columns medium-12'>
                       <form>
                           <!-- BASIC FORM LAYOUT FOR THE PAGE -->
                           <div class='row'>
                               <div class='columns medium-4'>

                                   <label>First Name @Html.EditorFor(t => t[i].FirstName)</label>
                               </div>
                               <div class='columns medium-4'>
                                   <label>Last Name @Html.EditorFor(t => t[i].LastName)</label>
                               </div>
                               <div class='columns medium-4'>
                                   <label>PTIN @Html.EditorFor(t => t[i].PTIN)</label>
                               </div>
                           </div>
                           <div class='row'>
                               <div class='columns medium-4'>
                                   <label>Email Address @Html.EditorFor(t => t[i].Email)</label>
                               </div>
                               <div class='columns medium-4'>
                                   <label>Phone Number @Html.EditorFor(t => t[i].Phone)</label>
                               </div>
                               <div class='columns medium-4'>
                                   <p>&nbsp;</p>
                               </div>
                           </div>
                       </form>
                   </div>
               }
            }
        </div>
        <div class='columns medium-3' id="purchase-summary">
            <!-- PURCHASE SUMMARY -->
            <h2>Summary:</h2>
            <table>
                <tr>
                    <td>
                        <p>Products:</p>
                    </td>
                    <td>
                        <p>4</p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <p>Subtotal:</p>
                    </td>
                    <td>
                        <p>$906.00</p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <p>Discounts:</p>
                    </td>
                    <td>
                        <p>$0.00</p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h4>Total:</h4>
                    </td>
                    <td>
                        <h4>$906.00</h4>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div id="registration-buttons">
        <div class='row'>
            <div class='columns medium-9'>
                <hr id="last-divider">
                <div class='columns medium-6 small-6'>
                    <div id="registration-back">
                        <a href="#" class="course-link arrow-left">Back</a>
                    </div>
                </div>
                <div class='columns medium-6 small-6'>
                    <div class='float-right'>
                        @*<a class='button' href="@allWebinars">continue</a>*@
                        <button type="submit" id="submit">continue</button>
                    </div>
                </div>
            </div>
            <div class='columns medium-3'>&nbsp;</div>
        </div>
    </div>
</div>

编辑:我想出了如何修复它。我在Html.BeginForm()方法中有表单标签(我不想要)。事后看来,有意义的是,正在创建多个表单,但只能提交第一个表单。代码的工作版本如下。谢谢你的帮助,斯蒂芬:)

新视图:

@model IList<SitefinityWebApp.Mvc.Models.CheckoutViewModel>
@{
    var allWebinars = "http://" + Request.Url.Authority + "/CheckOut";
}


@Html.BeginForm(){
<div id="registration-content">
    <div class='row'>
        <div class='columns medium-12'>
            <h1>Attendee Information</h1>
        </div>
    </div>
    <div class='row'>
        <div class='columns medium-9'>
            <div class='columns medium-12'>
                <p class='instructions'>Please complete attendee information for the following courses:</p>
            </div>

            @foreach (SitefinityWebApp.Mvc.Models.CheckoutViewModel title in ViewBag.Titles)
            {
                var url = "http://" + Request.Url.Authority + "/webinar-product-details/" + title.Sku;
                 <div class='columns medium-12'>
                    <!--FIRST PRODUCT DESCRIPTION-->
                    <div class="product-description">
                        <p class='course-link'><a href="@url">@title.Title</a></p>
                        @*@Html.HiddenFor(t => t[i].Title)*@
                        @*<p class='small'>Accounting</p>*@
                        <p class='small'></p>
                    </div>
                     <br />
                </div>
            for (int i = 0; i < Model.Count; i++)
            {
                @Html.HiddenFor(t => t[i].Title)
                @Html.HiddenFor(t => t[i].Type)
                @Html.HiddenFor(t => t[i].IRSCourseNumber)
                @Html.HiddenFor(t => t[i].Id)
                @Html.HiddenFor(t => t[i].Sku)
                    if(Model[i].Title == title.Title){

                <div class='columns medium-12'>
                    <!-- BASIC FORM LAYOUT FOR THE PAGE -->
                    <div class='row'>
                        <div class='columns medium-4'>

                            <label>First Name @Html.EditorFor(t => t[i].FirstName)</label>
                            @Html.ValidationMessageFor(t => t[i].FirstName)
                        </div>
                        <div class='columns medium-4'>
                            <label>Last Name @Html.EditorFor(t => t[i].LastName)</label>
                            @Html.ValidationMessageFor(t => t[i].LastName)
                        </div>
                        @if (Model[i].IRSCourseNumber != null && Model[i].IRSCourseNumber != "")
                        {
                        <div class='columns medium-4'>
                            <label>PTIN @Html.TextBoxFor(t => t[i].PTIN, new { @class = "ptin" })</label>
                            @Html.ValidationMessageFor(t => t[i].PTIN)
                        </div>
                        }else{
                            <div class='columns medium-4'>
                            <label>&nbsp;</label>
                        </div>
                        }
                    </div>
                    <div class='row'>
                        <div class='columns medium-4'>
                            <label>Email Address @Html.EditorFor(t => t[i].Email)</label>
                            @Html.ValidationMessageFor(t => t[i].Email)
                        </div>
                        <div class='columns medium-4'>
                            <label>Phone Number @Html.TextBoxFor(t => t[i].Phone, new { @class="phone"})</label>
                            @Html.ValidationMessageFor(t => t[i].Phone)
                        </div>
                        <div class='columns medium-4'>
                            <p>&nbsp;</p>
                        </div>
                    </div>
                    <br />
                    <br />
                </div>
            }
            }
            }

        </div>
        <div class='columns medium-3' id="purchase-summary">
            <!-- PURCHASE SUMMARY -->
            <h2>Summary:</h2>
            <table>
                <tr>
                    <td>
                        <p>Products:</p>
                    </td>
                    <td>
                        <p>4</p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <p>Subtotal:</p>
                    </td>
                    <td>
                        <p>$906.00</p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <p>Discounts:</p>
                    </td>
                    <td>
                        <p>$0.00</p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h4>Total:</h4>
                    </td>
                    <td>
                        <h4>$906.00</h4>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div id="registration-buttons">
        <div class='row'>
            <div class='columns medium-9'>
                <hr id="last-divider">
                <div class='columns medium-6 small-6'>
                    <div id="registration-back">
                        <a href="#" class="course-link arrow-left">Back</a>
                    </div>
                </div>
                <div class='columns medium-6 small-6'>
                    <div class='float-right'>
                        @*<a class='button' href="@allWebinars">continue</a>*@
                        <button class="submitButton" type="submit" id="submit">continue</button>
                    </div>
                </div>
            </div>
            <div class='columns medium-3'>&nbsp;</div>
        </div>
    </div>

</div>
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.3.1/jquery.maskedinput.min.js">

</script>
<script>
    jQuery(function ($) {
        $(".phone").mask("999-999-9999");
        $(".ptin").mask("99999999");
    });

</script>

新控制器:

[HttpPost]
    public ActionResult Index(IList<CheckoutViewModel> cvm)
    {
        int viewmodelerror = 0;
        foreach(CheckoutViewModel c in cvm){
            if (ModelState.IsValid)
            {
                //if (c.PTIN != null || c.PTIN != "")
                    //return Redirect("~/CheckOut");
            }
            else
            {
                viewmodelerror++;
                //return View(cvm);
            }
        }

        return View(cvm);
            //return new EmptyResult();
    }

0 个答案:

没有答案