簡體   English   中英

我希望ListBoxFor中存在的所有項目自動進入控制器

[英]I want all items present in ListBoxFor to controller in automatically

這是我的模型:

public class ReportFilterEditViewModel : ViewModelBase
{
    /..../
    public List<string> IncludedProducts { get; set; }
}

這是我的看法

@Html.ListBoxFor(model => model.IncludedProducts,new SelectList(Model.IncludedProducts, "Name")})
<input type="submit" value="Save" id="saveFilter" class="button" />

關鍵是...我想將列表中的所有元素都傳遞給控制器​​,而不選擇它們。 請幫幫我! 提前致謝。 伊戈爾

//首先在您的View中使用Javascript執行以下操作:

 var data = [@Html.Raw(String.Join(",", Model.IncludedProducts.Select(i => "'" + i + "'")))];

//Pass the data array to the controller 

  $.ajax({
                type: 'POST',
                url: '@Url.Action("GetProducts", "Home")',
                contentType: 'application/json',
                data: JSON.stringify(data),
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert('Error while communicating with controller!');
                    }
            }).done(function (result) {
               // console.log(result.Success);
                    if (result.Success == 'true') {
                        console.log('sent to the controller');

                    }
                    else {

                        console.log("error sending data");
                    }
                });


//then in your Controller
[HttpPost]
Public JsonResult GetProducts(List<string> model)
{

//do whatever you want with the model coz that's your product list


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM