简体   繁体   English

@ Html.EditorFor将错误的值发送到控制器

[英]@Html.EditorFor sends wrong value to the controller

I created @Html.EditorFor for ICollection list which looks like this. 我为ICollection列表创建了@Html.EditorFor ,如下所示。

 public virtual ICollection<DescriptionParameters> DescriptionParameters { get; set; }

Class DescriptionParameters shown below. 类说明参数如下所示。

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; }
    }

I created @Html.EditorFor(x => x.DescriptionParameters,new {Id="DescriptEditor" }) inside Html form . 我在HTML表单内创建了@Html.EditorFor(x => x.DescriptionParameters,new {Id="DescriptEditor" }) For this Editor i created EditorForTemplate. 我为此编辑器创建了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>

I want to make next behavior for my application(see screenshot):When it is pressed second "-" button,it should deletes second element in the list,instead of ,it always deletes first element in ICollection list in spite of my choice. 我想为我的应用程序做下一个动作(请参见屏幕截图):当按下第二个“-”按钮时,它应该删除列表中的第二个元素,而不是总是选择删除ICollection列表中的第一个元素。 视图中的EditorFor

For this reason i use DiscriptionParameters script.Which looks like this. 出于这个原因,我使用DiscriptionParameters script.Which看起来是这样的。

$('.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)
        }
    })
})

Which sends data to the RemoveDescriptionParameter action method. 它将数据发送到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 method for description and descriptionParameter parameters,i get values of first element in list,instead of chosed list element. 在用于descriptiondescriptionParameter参数的RemoveDescriptionParameter方法中,我获取列表中第一个元素的值,而不是选择的列表元素。

Look at this line in your code. 查看代码中的这一行。

"&description=" + $('.EnterDescriptionInfoField').val() “&description =” + $('。EnterDescriptionInfoField')。val()

Your jQuery selector is the css class. 您的jQuery选择器是css类。 Your razor code will more than one input fields with this css class ( Just check the view source of your page and you will see it ) when your DescriptionParameters contains more than one item. DescriptionParameters包含多个项目时,您的razor代码将使用此css类提供多个输入字段( 只需检查页面的查看源,您将看到它 )。

jQuery val() method returns you the value of the first element in the set of matched elements . jQuery val()方法返回匹配元素集中第一个元素的值 That means, If you had 3 items in your DescriptionParameters property, your razor code create 3 table and you will have three inputs with css class name EnterDescriptionInfoField . 这意味着,如果您的DescriptionParameters属性中包含3个项目,那么您的剃刀代码将创建3个表,并且您将拥有三个输入,其类名称为EnterDescriptionInfoField $('.EnterDescriptionInfoField') will give you an array of 3 inputs matching the jQuery selector and val() will return the value of first item in the array ! $('.EnterDescriptionInfoField')将为您提供3个与jQuery选择器匹配的输入数组,而val()将返回数组中第一项的值!

You should be using relative jQuery selector(s). 您应该使用相对的jQuery选择器。 You may use the jQuery closest method to get the current table row and use find method to get the input with the specific css class. 您可以使用jQuery closest方法来获取当前表行,并使用find方法来获取具有特定css类的输入。

This should work. 这应该工作。

$(function(){

  $('.RemoveDescription').click(function() {

     $.ajax({
             url: '/AddProductsDialog/RemoveDescriptionParameter',
             context: this,
             type: 'GET',
             data: $('#AddProdForm').serialize() +
             "&description=" +
                            $(this).closest("tr").find('.EnterDescriptionInfoField').val() +
             "&descriptionParametr=" +
                    $(this).closest("tr").find('.EnterDescriptionParameterInfoField').val(),
             success: function(product) {
                //do something with the response
             }
     });

  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM