将项目列表从$ .get()传递给MVC控制器

时间:2016-08-26 03:53:19

标签: javascript c# jquery asp.net-mvc

我的controller Action method如下所示:

    [HttpGet]
    [Route("ShowModal")]
    public Task<IActionResult> GetDetails(int id, string name, IEnumerable<Employee> employees)
    {
    //create a model
    //Some business logic codes

    return PartialView("_Partial.cshtml", model);
    }

我需要在按下按钮时从jQuery's $.get()方法调用上面的Action方法,捕获返回为HTML的部分视图,并在Bootstrap弹出窗口中显示它。

我无法通过IEnumerable<Employee>方法传递jQuery,它总是null,无论我尝试什么。

以下是JS代码:

<a class="btn btn-primary" onclick="ShowModal();" data-keyboard="true" data-toggle="modal">ShowModal</a>

<div class="modal fade" id="divShowModalDialog" role="dialog" tabindex="-1">
    <div class="modal-body" id="divShowModalBody">
    </div>
</div>

function ShowModal()
    {
        var list = [{ Id: 101, Gender: 'MALE' }, { Id: 102, Gender: 'FEMALE' }];
        list = JSON.stringify(list);

        var data = { 'id': 999, 'name': 'JAMES', 'employees': list };

        $.get('/Area1/Controller1/ShowModal', data)
        .done(function (response) {
            if (response != undefined) {
                $('#divShowModalBody').html(response);
                $('#divShowModalDialog').modal(
                    {
                        backdrop: 'static',
                        keyboard: true,
                    });                
            }
        })
        .fail(function (xhr) {
            console.log(xhr);
        })           
    }

我在Action方法中获得idname参数,但列表始终为空。我在删除JSON.stringify()之后尝试过,但它不起作用。 我知道我错过了一件小事,请帮忙。

3 个答案:

答案 0 :(得分:0)

首先,您应该在控制器操作上使用public void changeLocale() { Resources res = getResources(); // Change locale settings in the app. DisplayMetrics dm = res.getDisplayMetrics(); android.content.res.Configuration conf = res.getConfiguration(); conf.locale = new Locale("hi_IN"); res.updateConfiguration(conf, dm); setContentView(R.layout.xxx); } 而不是[HttpPost],当然您需要使用jQuery中使用$.post()的帖子,这是因为“POST”是实际将数据发布到服务器端的正确 - 但不是唯一 - HTTP动词。

其次,在将员工列表放入要发送的[HttpGet] javascript对象之前,不应该对其进行字符串化。 所以, data ,马上就去吧

list = JSON.stringify(list);

您可能还需要使用上面链接中的var data = { 'id': 999, 'name': 'JAMES', 'employees': list }; 检查文档提供dataType。

最后,在您的操作方法中删除$.post(url,data,onsucess,dataType)并将其替换为具体的集合类型,如IEnumerable<T>,因为JSON序列化程序需要知道在绑定时实例化哪种类型的集合。

答案 1 :(得分:0)

实际上,您可以通过使用$ .ajax()

将其更改为POST来实现它

在操作方法

中使用字典对象而不是IEnumerable
public ActionResult GetDetails(int id, string name, Dictionary<int,string> employees)
    {

然后在剧本中

var list = [{ Id: 101, Gender: 'MALE' }, { Id: 102, Gender: 'FEMALE' }];


    var data = { id: 999, name: 'JAMES', employees: list };
    debugger;
    $.ajax({
        url: '/Home/GetDetails',
        type: "GET",
        data :data,
        contentType: "application/json",
        dataType: "json"
    });

答案 2 :(得分:-1)

我正在回复你的评论,但我认为更容易证明我的观点是一个答案。

要回答你的问题,不,我不确定。但这就是为什么我要求你先尝试一下,这似乎是合乎逻辑的,因为你将list而不是IEnumerable传递给你的函数。

另外,根据您的Employee课程的样子,您应该尝试这样做:(您的Employee课程中需要构造函数

List<Employee> list = new List<Employee>();
list.Add(new Employee(101, 'MALE'));
list.Add(new Employee(102, 'FEMALE'));

var data = { 'id': 999, 'name': 'JAMES', 'employees': list };
...

<强>更新

我意识到为什么我错了,我一直在用C#术语思考。 Json.stringify()返回一个json样式的字符串(C#只是看作一个字符串),所以你的public Task GetDetails(int id, string name, IEnumerable employees)应该是public Task GetDetails(int id, string name, string employees),然后在C#中,你需要解析JSON字符串。一个有用的链接: How can I parse JSON with C#?

相关问题