【发布时间】: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