Ich bin ziemlich neu in MVC, aber ich habe versucht, eine Dropdown-Liste in einer Ansicht zu verwenden, die ein Modell vom Typ List <> hat. Der Grund dafür ist, mehrere Datensätze auf einer einzelnen Seite zu erstellen.mvc Dropdown-Liste mit Modell der Typenliste
Ich konnte erfolgreich eine Ansicht erstellen, die nicht vom Typ List <> und einen einzelnen Datensatz zu einem Zeitpunkt ist, aber trotz viel Googlen kann die Lösung nicht implementieren, wenn ich die Ansicht mit einem Modell implementieren vom Typ Liste <>.
Modell
public class Product
{
public int ProductID { get; set; }
public string Description { get; set; }
public int VatRateID { get; set; }
[ForeignKey("VatRateID")]
public virtual VatRate VatRate { get; set; }
}
-Controller
// GET: /Product/Bulkdata
public ActionResult BulkData()
{
PopulateVatRateDropDownList();
List<Product> model = new List<Product>();
model.Add(new Product
{
ProductID = 0,
Description = "",
VatRateID = 1
}
);
return View(model);
}
//
// POST: /Product/Bulkdata
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult BulkData(
[Bind(Include = "Description,VatRateID")]
List<Product> products)
{
try
{
if (ModelState.IsValid)
{
foreach (var p in products)
{
db.Product.Add(p);
}
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DataException)
{
ModelState.AddModelError("", "Bulk - Unable to save changes, Try again, and if the probelm persists contact your administrator");
}
PopulateVatRateDropDownList();
return View(products);
}
private void PopulateVatRateDropDownList(object selectedVatRate = null)
{
var vatRateQuery = from v in db.VatRate
select v;
ViewBag.VatRateID = new SelectList(vatRateQuery, "VatRateID", "VatRateDescription", selectedVatRate);
}
Ansicht
@if (Model != null && Model.Count > 0)
{
int j = 0;
foreach (var i in Model)
{
<tr style="border:1px solid black">
<td>
@Html.EditorFor(m => m[j].Description)
@Html.ValidationMessageFor(m => m[j].Description)
</td>
<td>
@Html.DropDownList("VatRateID", String.Empty)
@Html.ValidationMessageFor(m => m[j].VatRateID)
</td>
<td>
@if (j > 0)
{
<a href="#" class="remove">Remove</a>
}
</td>
</tr>
j++;
}
}
Wenn ich die Anwendung mit diesem Code ausführen, dann ist 0 bis vatRateID immer vorüber. Ich habe zahlreiche andere Lösungen als die oben genannten einschließlich ASP.NET MVC DropDownListFor with model of type List<string> versucht. Wie ich bereits erwähnt habe, bin ich neu bei mvc, daher weiß ich, dass es wahrscheinlich etwas Einfaches gibt, das mir fehlt. Jede Hilfe würde sehr geschätzt werden.
Dank
Sie können eine' foreach'-Schleife nicht verwenden. Siehe [diese Antwort] (http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –
Und Ihre Verwendung von 'DropDownList()' macht keinen Sinn - es Bindung an 'VatRateID', aber Ihr Modell ist' List 'und' List 'enthält keine Eigenschaft namens' VatRateID'. Sie schließen auch alles aus, indem Sie das '[Bind]' - Attribut binden. –
Danke Stephen, kannst du mir bitte ein Beispiel dafür geben, wie ich DropDownList() in der obigen – user2046411