我有一个场景,我需要在一个表中存储多行。让我详细解释一下。
我有表格 Price
,其 4列,ID, ModelID, AppSettingID,Amount
。
我正在寻找向表格中插入多个值
我已将AppSettingID绑定到视图上的不同组合框,因为我将其分类到数据库中。
这就是我现在正在做的事情。
查看:
<div class="editor-label">
@Html.LabelFor(model => model.ModelID, "Model")
</div>
<div class="editor-field">
@Html.DropDownList("ModelID", String.Empty)
@Html.ValidationMessageFor(model => model.ModelID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AppsettingID, "Mobile Condition")
</div>
<div class="editor-field">
@Html.DropDownList("Mobile Condition", new SelectList(ViewBag.ConditionID, "Text", "Value"))
@Html.ValidationMessageFor(model => model.AppsettingID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Amount)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Amount)
@Html.ValidationMessageFor(model => model.Amount)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AppsettingID, "Tennure")
</div>
<div class="editor-field">
@Html.DropDownList("Tennure", new SelectList(ViewBag.AppsettingID, "TexT", "Value"))
@Html.ValidationMessageFor(model => model.AppsettingID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Amount)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Amount)
@Html.ValidationMessageFor(model => model.Amount)
</div>
<p>
<input type="submit" value="Create" />
</p>
控制器:
public ActionResult Create()
{
//ViewBag.AppsettingID = db.Appsettings.Select(r => r.ID).Distinct();
ViewBag.ModelID = new SelectList(db.Models, "ID", "Name");
//ViewBag.Tennure = db.Appsettings.Select(s => s.Type == "Tennure").Distinct();
IQueryable<Appsetting>TennureIDs = db.Appsettings.Where(s => s.Type == "Tennure").Distinct();
IQueryable<Appsetting> Conditions = db.Appsettings.Where(s => s.Type == "Mobile Condition").Distinct();
List<SelectListItem> items = new List<SelectListItem>();
foreach (var t in TennureIDs)
{
SelectListItem s = new SelectListItem();
s.Text = t.ID.ToString();
s.Value = t.Value.ToString();
items.Add(s);
}
ViewBag.AppsettingID = items;
List<SelectListItem> conds = new List<SelectListItem>();
foreach (var t in Conditions)
{
SelectListItem s = new SelectListItem();
s.Text = t.ID.ToString();
s.Value = t.Value.ToString();
conds.Add(s);
}
ViewBag.ConditionID = conds;
return View();
}
//
// POST: /Price/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Price price, FormCollection form)
{
if (ModelState.IsValid)
{
int test = Convert.ToInt16(form["Mobile Condition"]);
price.AppsettingID = test;
db.Prices.Add(price);
db.SaveChanges();
return RedirectToAction("Index");
}
//ViewBag.AppsettingID = new SelectList(db.Appsettings, "ID", "Type", price.AppsettingID);
//ViewBag.ModelID = new SelectList(db.Models, "ID", "Name", price.ModelID);
return View(price);
}
答案 0 :(得分:3)
我希望这个解决方案可以帮到你
以下是剃刀页面,这里我以表格格式显示模型
@model List<MyModel>
<h2>Create</h2>
@using (Html.BeginForm())
{
<table>
<tr>
<th>
Model ID
</th>
<th>
App Setting ID
</th>
<th>
Amount
</th>
</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.TextBoxFor(x => Model[i].ModelID)
</td>
<td>
@Html.TextBoxFor(x => Model[i].AppsettingID)
</td>
<td>
@Html.TextBoxFor(x => Model[i].Amount)
</td>
</tr>
}
</table>
<input type="submit" />
}
当用户点击提交按钮时,它会将模型传递给控制器
<强>控制器强>
[HttpPost]
public ActionResult Create(List<MyModel> m)
{
// Do Coding
return View("Create", m);
}