代码之家  ›  专栏  ›  技术社区  ›  toy

如何在aspmvc4中从局部视图中的数据库中构建dropdownlistfor

  •  0
  • toy  · 技术社区  · 11 年前

    当我构建mvc项目时,我还没有在asp.net mvc4.5中完成它们
    所以,如果这个问题看起来很明显,请原谅我
    或者如果我犯了明显的错误
    请随时更正所有问题。。。

    事实上,我整个周末都在谷歌上搜索,似乎得到了一些答案
    但还没能把这一切拼凑起来

    正如标题所说,我试图为
    其中数据是从数据库中提取的
    增加另一层复杂性
    这个DDLF是局部视图

    所以这就是我得到的

    型号:

    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity;
    using System.Globalization;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Security;
    
    namespace proj.Models
    {
    public class chartType
    {
        public int id { get; set; }
    
        [Required]
        public string typename { get; set; }
    }
    
    public class chartTypeVM
    {
        [Display(Name = "selectedID")]
        public int selectedChartType { get; set; }
    
        public IEnumerable<SelectListItem> List { get; set; }
    }
    }
    

    控制器:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    using proj.Models;
    
    namespace proj.Controllers
    {
    public class HomeController : Controller
    {
        orojDB db = new orojDB ();
    
        public ActionResult Index()
        {
            chartTypeVM model = initialiseSM();            
    
            return View(model);
        }
    
        private chartTypeVM initialiseSM()
        {
    
            // make scenario DDL
            // using chartTypes for now
            var q = db.chartTypes
                .Select(ct=> new {
                    ct.id,
                    ct.typename
                })
                .AsEnumerable()
                .Select(ct => new SelectListItem {
                    Value = ct.id.ToString(),
                    Text = ct.typename
                });
    
            var cts = new chartTypeVM{
                List = q.AsEnumerable()
            };
            cts.selectedChartType = 1;
    
            Debug.WriteLine("asdfasdf");
    
            return cts;
        }
    }
    }
    

    视图:

    @using proj.Models
    @model chartTypeVM
    
    <h3>We suggest the following:</h3>
    
    @* *************** sections *************** *@
    @section sm {
            <div id="sm">
                <section class="content-wrapper sm-content">
                    @Html.Partial("_smPartial", Model)
                </section>
                <div class="smCloseD"><img src="/Images/closeArrow.png" alt="Open Scenario Manager" /></div>
                <div class="clear"></div>
            </div>
    }
    

    主布局(大部分未显示):

    @using proj.Models
    @model chartTypeVM
    
                <section class="content-wrapper main-content">
                    @RenderBody()
                </section>
    
            @RenderSection("sm",false);
    

    最后是部分:

    @using proj.Models
    
    @model chartTypeVM
    <section class="smVars">
    <div class="varLabel">Scenario:</div>
    <div class="varInput">@Html.DropDownListFor( model=>model.selectedChartType, (SelectList)model.List)</div>
    </section>
    



    用我尝试过的东西
    我犯了这样的错误:
    -名称“model”在当前上下文中不存在
    -或者关于不能使用动态变量的问题
    -或者正在推断的东西

    不管怎样,如果你能帮忙,我会非常感激
    =:-)
    谢谢

    1 回复  |  直到 11 年前
        1
  •  0
  •   Darin Dimitrov    11 年前

    为了使用 DropDownListFor helper需要将视图强类型化为模型。在你的情况下,那就是 chartTypeVM 班所以添加一个 @model 指向局部视图顶部的指示,指示正在使用的视图模型:

    @model chartTypeVM
    <section class="smVars">
        <div class="varLabel">Scenario:</div>
        <div class="varInput">
            @Html.DropDownListFor(model => model.selectedChartType, (SelectList)Model.List)
        </div>
    </section>