ICollection
リストのために@Html.EditorFor
を作成しました。@ Html.EditorForはコントローラーに間違った値を送信します
public virtual ICollection<DescriptionParameters> DescriptionParameters { get; set; }
クラス説明次のパラメータを示します。
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; }
}
私はをHtml形式で作成しました。 このエディタではEditorForTemplateを作成しました。
@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>
は私が(スクリーンショットを参照)自分のアプリケーションのための次の行動を作りたい:秒押すと、「 - 」ボタン、それは代わりに、リストの2番目の要素を削除する必要があり、それは常にICollection
の最初の要素を削除します私の選択にもかかわらずリスト。
このため、私はDiscriptionParameters
スクリプトを使用しています。このように見えます。 RemoveDescriptionParameterアクションメソッドにデータを送信し
$('.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)
}
})
})
。 description
と descriptionParameter
パラメータの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);
}
、私の代わりにchosedリスト要素のリストの最初の要素の値を取得します。