Ich erstellte @Html.EditorFor
für ICollection
Liste, die so aussieht.@ Html.EditorFor sendet falschen Wert an den Controller
public virtual ICollection<DescriptionParameters> DescriptionParameters { get; set; }
KlassenbeschreibungParameter siehe unten.
public partial class DescriptionParameters
{
public int Id { get; set; }
[Required (ErrorMessage = "Please enter description")]
public string Description { get; set; }
[Required(ErrorMessage = "Please enter description parameter")]
public string DescriptionParameter { get; set; }
public int Product_Id { get; set; }
public virtual Product ProductSet { get; set; }
}
Ich erstellte @Html.EditorFor(x => x.DescriptionParameters,new {Id="DescriptEditor" })
in HTML-Form. Für diesen Editor habe ich EditorForTemplate erstellt. „-“ Taste, soll die zweite Element löscht in der Liste, statt es immer löscht erstes Element in ICollection
Wenn er gedrückt wird Sekunde:
@model OnlineShop.Models.DescriptionParameters
<script src="~/Scripts/DiscriptionParameters.js" type="text/javascript"></script>
<table>
<tr>
<td>
@Html.TextBoxFor(x => x.Description, new { Id="DescriptionField",Class = "EnterDescriptionInfoField" })
</td>
<td>
@Html.TextBoxFor(x => x.DescriptionParameter, new { Id = "DescriptionParamField", Class = "EnterDescriptionParameterInfoField" })
</td>
<td>
<input class="AddDescription" type="button" value="+"
style="width:20px;height:20px" />
</td>
<td>
<input class="RemoveDescription" type="button" value="-"
style="width:20px;height:20px;text-align:center" />
</td>
</tr>
</table>
<table>
<tr>
<td>
@Html.ValidationMessageFor(x => x.Description)
</td>
<td style="padding-left:10px">
@Html.ValidationMessageFor(x => x.DescriptionParameter)
</td>
</tr>
</table>
ich als nächstes Verhalten für meine Anwendung (siehe Screenshot) machen will Liste trotz meiner Wahl.
Aus diesem Grund verwende ich DiscriptionParameters
script.Which sieht so aus.
$('.RemoveDescription').click(function() {
$.ajax({
url: '/AddProductsDialog/RemoveDescriptionParameter',
context: this,
type: 'GET',
data: $('#AddProdForm').serialize() + "&description="
+ $('.EnterDescriptionInfoField').val() + "&descriptionParametr="
+ $('.EnterDescriptionParameterInfoField').val(),
success: function (product) {
$('#ProgressShow').empty()
$('#AddProdForm').replaceWith(product)
}
})
})
Das sendet Daten an die Aktionsmethode RemoveDescriptionParameter.
[HttpGet]
public ActionResult RemoveDescriptionParameter(Product product,string description,
string descriptionParameter)
{
if(description=="")
{
description = null;
}
if (descriptionParametr == "")
{
description = null;
}
if (product.DescriptionParameters.Count > 1)
{
product.DescriptionParameters.Remove(
product.DescriptionParameters.FirstOrDefault(x=>x.Description==description
&& x.DescriptionParameter==descriptionParametr));
}
GoodsContainer1 goods = new GoodsContainer1();
ViewData["Categories"] = goods.CategorySet;
ViewData["SubCategories"] = goods.SubCategorySet;
return PartialView("~/Views/AddProductsDialog/AddProducts.cshtml", product);
}
In RemoveDescriptionParameter Verfahren für description
und descriptionParameter
Parameter, erhalte ich Werte des ersten Elements in der Liste, statt chosed Listenelement.