如何在MVC3中将数据保存到DataTable中?

时间:2012-04-18 15:58:55

标签: asp.net-mvc asp.net-mvc-3 model-view-controller

我有mvc3应用程序在这里我使用了两个局部视图1.controls 2.webgrid

内部控件我正在填充实际数据库表中的下拉列表。使用EF

在index.cshtml上我有一个表单需要从这些下拉列表中选择值,当按下插入按钮时,这些值应该转到Temp“DataTable”并在webgrid中显示它...我是新手到MVC3并且不知道如何做到这一点。

Controls.cshtml
@model Mapping.Models.SecurityIdentifierMappingViewModel
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Mapping</legend>
        <div class="editor-label">
            @Html.Label("Pricing SecurityID")
        </div>
        <div class="editor-field">
            @Html.HiddenFor(model => model.MappingControls.Id)
            @Html.DropDownListFor(model => model.MappingControls.PricingSecurityID,
         new SelectList(Model.PricingSecurities, "Value", "Text"),
         "Select SecurityID"
            )
            @Html.ValidationMessageFor(model => model.MappingControls.PricingSecurityID)
        </div>
        <div class="editor-label">
            @Html.Label("CUSIP ID")
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.MappingControls.CUSIP,
         new SelectList(Model.CUSIPs, "Value", "Text"),
            "Select CUSIP"
            )
            @Html.ValidationMessageFor(model => model.MappingControls.CUSIP)
        </div>

        <div class="editor-label">
            @Html.Label("Calculation")
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.MappingControls.Calculation)
            @Html.ValidationMessageFor(model => model.MappingControls.Calculation)
        </div>
        <p>
            <input id="btnsubmit" type="submit" value="Insert" />

        </p>
    </fieldset>
}

HomeController.cs
public class HomeController : Controller
    {
        //
        // GET: /Home/

        mydataEntities dbContext = new mydataEntities();
        DataRepository objRepository = new DataRepository();

        //GET

        public ActionResult Index(string userAction , int uid = 0)
        {
            var mappingobj = new SecurityIdentifierMappingViewModel();
            mappingobj.MappingWebGridList = dbContext.SecurityIdentifierMappings.ToList();

                mappingobj.MappingControls = new SecurityIdentifierMapping();
                mappingobj.MappingControls.PricingSecurityID = 0;
                mappingobj.MappingControls.CUSIP = string.Empty;

            mappingobj.PricingSecurities = objRepository.GetPricingSecurityID();
            mappingobj.CUSIPs = objRepository.GetCUSIP();

            return View(mappingobj);
        }


        //POST

        [HttpPost]
        public ActionResult Index(SecurityIdentifierMappingViewModel objModel)
        {


            if (objModel.MappingControls.Id > 0)
            {
                if (ModelState.IsValid)
                {
                    dbContext.Entry(objModel.MappingControls).State = EntityState.Modified;
                    try
                    {
                        dbContext.SaveChanges();
                        //objModel = new SecurityIdentifierMappingViewModel();
                        //return RedirectToAction("Index", "Home");
                    }
                    catch (System.Data.Entity.Validation.DbEntityValidationException ex)
                    {
                        throw;
                    }
                }

            }

            //insert code
            else
            {
                if (ModelState.IsValid)
                {
                    dbContext.SecurityIdentifierMappings.Add(objModel.MappingControls);
                    try
                    {
                        dbContext.SaveChanges();
                    }
                    catch (System.Data.Entity.Validation.DbEntityValidationException ex)
                    {
                        throw;
                    }
                }

            }

            return RedirectToAction("Index");
        }
    }

public class SecurityIdentifierMappingViewModel
{
    public IEnumerable<SecurityIdentifierMapping> MappingWebGridList { get; set; }
    public SecurityIdentifierMapping MappingControls { get; set; }

    public List<SelectListItem> PricingSecurities { get; set; }
    public List<SelectListItem> CUSIPs { get; set; }
}

目前使用SecurityIdentifierMapping作为数据库中的第3个表,其中插入我的表单数据...但需要将其插入“DataTable”

1 个答案:

答案 0 :(得分:1)

您必须创建一个DataTable对象并为其分配适当的DataColumn对象。之后,将SecurityIdentifierMapping属性映射到临时数据表中的列。至于将DataTable映射到WebGrid,我不会说它不可能,因为我从未亲自尝试过这个东西,但你必须将它映射回SecurityIdentifierMapping的集合。

但是,为什么需要DataTable? DataTable有哪些可能的优势可以超过IQueryable或IEnumerable?你真的想用这个策略实现什么目标?

更新:

您已在ViewModel类(SecurityIndentifierMappingViewModel)中使用IEnumerable。同时,在POST到索引时将数据存储在数据库中,并在GET版本的索引中再次获取。

您缺少的是在视图中创建WebGrid对象。您的视图可以这样定义:

@{
    var columns = new List<string>();
    columns.Add("Column 1");
    columns.Add("Column 2");

    var grid = new WebGrid(model: Model.MappingWebGridList, columnNames: columns);
 }

 @grid.GetHtml()

将上述代码放在Index视图中的某个位置,并定义自己的列。另外,看一下我写的这篇文章,以便更多地了解你可以用WebGrid http://apparch.wordpress.com/2012/01/04/webgrid-in-mvc3/做些什么。

我希望我能帮助你至少一点。