似乎无法让我的下拉菜单工作,花了几个小时查看这个和一个教程,但我似乎无法正确

时间:2014-04-12 19:25:02

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

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

这是我的控制器代码:

    // 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);
    }

我的模特:

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>

1 个答案:

答案 0 :(得分:0)

似乎代码没有任何问题。

已过帐值Chrome posted value capture

的Chrome捕获 行动value at controller action

时的

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