【问题标题】:Cant seem the get my dropdown menu to work, spent hours looking through this and a tutorial, but I cant seem to get it right似乎无法让我的下拉菜单工作,花了几个小时浏览这个和一个教程,但我似乎无法正确
【发布时间】:2014-04-12 19:25:02
【问题描述】:

我正在尝试在我的其他文本输入旁边获得一个下拉菜单,我一直在跟踪有关此特定功能的音乐商店教程,但我似乎无法让我的工作。我一直在修改我的代码并比较了几个小时,但我看不到我的错误,如果有人可以提供帮助,将不胜感激。

这是我的控制器代码:

    // GET: /Default1/Create

    public ActionResult Create()
    {
        ViewBag.CardTypeId = new SelectList(db.CardTypee, "CardTypeId", "Type");
        return View();
    } 

    //
    // POST: /Default1/Create

    [HttpPost]
    public ActionResult Create(Card card)
    {
        if (ModelState.IsValid)
        {
            db.Cards.Add(card);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        ViewBag.CardTypeId = new SelectList(db.CardTypee, "CardTypeId", "Type",  card.CardTypeId);
        return View(card);
    }

我拥有的模型:

卡片.cs

 namespace ActualPayment.Models
 {
   public class Card
   {

    public int Id { get; set; }
    public CardType CardType { get; set; }
    public int CardTypeId { get; set; }
    public string Name { get; set; }
    public string CardNumber { get; set; }
    public int SortCode { get; set; }
    public int SecurityCode { get; set; }
    public int ExpirationDate { get; set; }

  }
}

CardType.cs

namespace ActualPayment.Models
{
    public partial class CardType
    {
        [Key] public int CardTypeId { get; set; }
        public string Type { get; set; }

    }
}   

CardTypes.cs

namespace ActualPayment.Models
{
    public class CardTypes : DropCreateDatabaseIfModelChanges<CardPayment>
   {
       protected override void Seed(CardPayment context)
       {
           var cardType = new List<CardType>
           {
               new CardType { Type = "Visa/Delta/Electron" },
               new CardType { Type = "Master Card/Euro Card" },
               new CardType { Type = "American Express" },
               new CardType { Type = "Solo/Maestro" },
               new CardType { Type = "Maestro" },
           };
        }
    }
}

CardPayments.cs

namespace ActualPayment.Models
{
   public class CardPayment : DbContext
    {

        public DbSet<CardType> CardTypee { get; set; }
        public DbSet<Card> Cards { get; set; }
    }
}

我的看法:

@model ActualPayment.Models.Card

@{
 ViewBag.Title = "Create";
}

<h2>Create</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>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Card</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.CardTypeId, "CardType")
    </div>
    <div class="editor-field">
        @Html.DropDownList("CardTypeId", String.Empty)
        @Html.ValidationMessageFor(model => model.CardTypeId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.CardNumber)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.CardNumber)
        @Html.ValidationMessageFor(model => model.CardNumber)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SortCode)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SortCode)
        @Html.ValidationMessageFor(model => model.SortCode)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SecurityCode)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SecurityCode)
        @Html.ValidationMessageFor(model => model.SecurityCode)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ExpirationDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ExpirationDate)
        @Html.ValidationMessageFor(model => model.ExpirationDate)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

【问题讨论】:

  • 可能是StyleSheets引起的问题,你也检查了吗?
  • 有一个下拉菜单,但是它很小,里面什么都没有。我尝试更改 css 以匹配那里的教程,但不幸的是它并没有产生任何影响。
  • 那么你的菜单代码在你的视图和控制器中在哪里?
  • 他们是不是已经发布了,还是你想要整个控制器类?那有标准的脚手架控件..但如果需要我可以。

标签: c# asp.net-mvc-3 visual-studio-2012 razor input


【解决方案1】:

好像代码没什么问题。

Chrome 捕获发布值

价值在行动

你的意思是 CardTypeId 没有被填充??

【讨论】:

  • 是的,有一个下拉菜单,但里面没有值,它只是一个带箭头的小框,只有一个可以选择的空白,会给出一个图像,但我不能。 .
  • 当用户登陆创建屏幕时,下拉菜单中没有值?你检查过 ViewBag.CardTypeId = new SelectList(db.CardTypee, "CardTypeId", "Type");即 db.CardTypee
  • 在我的控制器中我有: ViewBag.CardTypeId = new SelectList(db.CardTypee, "CardTypeId", "Type", card.CardTypeId);返回视图(卡片); },但我不是 100% 你的意思,在 mvc3 和 VS 方面非常业余
  • ViewBag.CardTypeId = new SelectList(db.CardTypee, "CardTypeId", "Type");意味着您将从 CardTypeId 设置下拉列表的值。在您的视图中,@Html.DropDownList("CardTypeId", String.Empty) 意味着 MVC 将查看 ViewBag/ViewData 以填充您的下拉值,string.Empty 是默认选择。您可以通过在其上放置断点来确认 ViewBag.CardTypeId 具有值吗?
  • 这是我想要设置的,还是应该使用 CardType 类中的 Type... 来完成?
猜你喜欢
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 2014-07-18
  • 1970-01-01
  • 2013-11-23
相关资源
最近更新 更多