-1

在我的 mvc3 应用程序中,我在 index() 上收到此错误,其中属性是类型的Nullable<int>,我无法将它们更改List<SelectListItem> 为我的方法返回类型,因为属性是由 EF 自动生成的,在这种情况下该怎么办请帮忙?

INDEX.CSHTML
@model Mapping.Models.SecurityIdentifierMappingViewModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>


@*Partial control for all controls*@

<div>
@Html.Partial("_ControlsPartial",Model.MappingControls)
</div>


<div>
@Html.Partial("_WebGridPartial",Model.MappingWebGridList)
</div>
public ActionResult Index()
    {

        var mappingobj = new SecurityIdentifierMappingViewModel();

        mappingobj.PricingSecurityID = objRepository.GetPricingSecurityID();
        mappingobj.CUSIP = objRepository.GetCUSIP();

        var viewModel = new SecurityIdentifierMappingViewModel
        {
            MappingControls = mappingobj.MappingControls,
            MappingWebGridList = dbContext.SecurityIdentifierMappings.ToList()
        };

        return View(viewModel);


    }

controlpartial.html
@model Mapping.Models.SecurityIdentifierMapping
@using (Html.BeginForm("Mapping", "Home"))
{
    @Html.DropDownList("SecurityID", Model.PricingSecurityID, "-- Select SecurityID --")
    <br />    
    @Html.DropDownList("CUSIPID", Model.PricingSecurityID, "-- Select CUSIPID --")
    <br />
    <button type="submit">Map</button>
}

webgridpartial.cshtml
     @model IEnumerable<Mapping.Models.SecurityIdentifierMappingViewModel>

@{
    ViewBag.Title = "Mapping";
    WebGrid grid = null;
    if (Model.Count() > 0 ){
    grid = new WebGrid(source: Model,
                            defaultSort: "Id",
                            canPage: true,
                            canSort: true,
                            rowsPerPage:20);
    }
}


<h2>Mapping</h2>


@if (grid != null)
{
@grid.GetHtml(
                tableStyle: "grid",
                headerStyle: "head",
                alternatingRowStyle: "alt",
                columns: grid.Columns(
                                            grid.Column("", header: null, format: @<text>@Html.ActionLink("Edit", "Edit", new { id = (int)item.id })  @Html.ActionLink("Delete", "Delete", new { id = (int)item.id })</text>),
                                            grid.Column("PricingSecurityID"),
                                            grid.Column("CUSIP")
                                          )

                )
}
<br />
<p>
    @Html.ActionLink("Back", "Index")
</p>

securityidentifiermapping.cs //auto generated by EF
 public partial class SecurityIdentifierMapping
    {
        public int Id { get; set; }
        public Nullable<int> PricingSecurityID { get; set; }
        public string Calculation { get; set; }
    }

public List<SelectListItem> GetPricingSecurityID()
        {
        var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing.AsEnumerable()
                                     select new SelectListItem
                                         {
                                                Text = m.PricingSecurityID.ToString(),
                                                Value = m.PricingSecurityID.ToString()
                                         }).AsEnumerable();

        return pricingSecurityID.ToList();
        }

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

        public List<SelectListItem> PricingSecurityID { get; set; }
        public List<SelectListItem> CUSIP { get; set; }
    }

采取List<SelectListItem>是因为想要填充下拉列表。那么在这种情况下我该怎么办?

4

1 回答 1

1

您必须在其中创建新属性ViewModel并将其设置List<SelectListItem>为该属性

public class SecurityIdentifierMappingViewModel
{
    public List<SelectListItem> PricingSecurityList{get;set;}

...
...
...
}

public ActionResult Index(){
.....
...
..
            var viewModel = new SecurityIdentifierMappingViewModel
            {
                MappingControls = mappingobj,
                MappingWebGridList = dbContext.SecurityIdentifierMappings.ToList(),
                PricingSecurityList = objRepository.GetPricingSecurityID();

            };
...
.....

}

看法

@Html.DropDownListFor(model=>model.MappingControl.PricingSecurityID, Model.PricingSecurityList)

或者

@Html.DropDownList("SecurityID", Model.PricingSecurityList, "-- Select SecurityID --")
于 2012-04-13T10:53:32.670 回答