用jQuery填充模型数据 [英] Populating Model Data with Jquery

查看:71
本文介绍了用jQuery填充模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个模型课,如下所示:

Hi,

I have a model class as below:

public class Location
{
  public int LocationId {get; set;}
  public int LocationName {get; set;}
}
public class Load
{
  public string LoadId {get; set;}
  public string LoadName {get; set;}
  public List<Location> Locations {get; set;}
}



我要完成的是:
1.我需要给出一个由Load类强类型化的索引视图
2.在客户端,Client按下一个按钮,然后$ .Ajax()函数从某处获取一些JSON响应
3.我需要把这个响应作为模型中的项放入:Model.Locations(这是一个列表)

我遇到的问题:
当Jquery收到响应时,我想将响应放入模型数据中.
1.放入模型(Model.Locations)的数据应为多选列表
2.我尝试过
@ Html.ListBox("Locations",ViewBag.LocationsList作为MultiSelectList)
甚至尝试将Json Response发送到服务器,然后尝试将数据放入模型中之后.

无论如何,我知道以上情况可能不是一个好主意.

我需要的是,我从某个地方获取JSON响应,我需要将其发送到服务器,让服务器执行一些工作,然后插入到可选列表中.

谁能指出我正确的方向?将会有很大的帮助.谢谢.

同样,这里唯一的问题是:将Jquery(Ajax/JSON)响应放入模型数据中,其中在填充数据之后,列表是multiselect列表,然后可以提交整个表单以进行处理.



What i''m trying to accomplish is :
1. I need to give an index view, strongly typed by Load class
2. On the client side, Client hits a button, and an $.Ajax() function gets some JSON response from somewhere
3. I need to put this response, as items which go into the model : Model.Locations (which is a list)

What i''m having issues with:
When Jquery gets a reponse, i want to put the response into Model data.
1. This data, put into model (Model.Locations) should be a multiple select list
2. I''ve tried
@Html.ListBox("Locations",ViewBag.LocationsList as MultiSelectList)
After even trying to send Json Response to server, and trying to put the data into model there.

Anyway, i know the above thing is probably bad idea.

What i need is, I get the JSON response from somewhere, i need to send it to the server, have the server do some work, and then insert into a selectable list.

Can anyone point me in the right direction? would be great help. Thanks.

Again, the only issue here is: putting Jquery (Ajax/JSON) response into model data, where in after populating data, the list is multiselect list, and then the whole form can be submitted for processing.

推荐答案

.Ajax()函数从某处获取一些JSON响应
3.我需要把这个响应作为模型中的项放入:Model.Locations(这是一个列表)

我遇到的问题:
当Jquery收到响应时,我想将响应放入模型数据中.
1.放入模型(Model.Locations)的数据应为多选列表
2.我尝试过
@ Html.ListBox("Locations",ViewBag.LocationsList作为MultiSelectList)
甚至尝试将Json Response发送到服务器,然后尝试将数据放入模型中之后.

无论如何,我知道以上情况可能不是一个好主意.

我需要的是,我从某个地方获取JSON响应,我需要将其发送到服务器,让服务器执行一些工作,然后插入到可选列表中.

谁能指出我正确的方向?将会有很大的帮助.谢谢.

同样,这里唯一的问题是:将Jquery(Ajax/JSON)响应放入模型数据中,其中在填充数据之后,列表为multiselect列表,然后可以提交整个表单以进行处理.
.Ajax() function gets some JSON response from somewhere
3. I need to put this response, as items which go into the model : Model.Locations (which is a list)

What i''m having issues with:
When Jquery gets a reponse, i want to put the response into Model data.
1. This data, put into model (Model.Locations) should be a multiple select list
2. I''ve tried
@Html.ListBox("Locations",ViewBag.LocationsList as MultiSelectList)
After even trying to send Json Response to server, and trying to put the data into model there.

Anyway, i know the above thing is probably bad idea.

What i need is, I get the JSON response from somewhere, i need to send it to the server, have the server do some work, and then insert into a selectable list.

Can anyone point me in the right direction? would be great help. Thanks.

Again, the only issue here is: putting Jquery (Ajax/JSON) response into model data, where in after populating data, the list is multiselect list, and then the whole form can be submitted for processing.


您好,

考虑到共享,这可能不是一种有效的方法.

尝试在索引的get部分中将数据加载到viewdata中,如下所示

Hi,

This may not be a efficient way to do it, thought to share it.

Try loading data in viewdata in the get part of index, like below

ViewData["CountryList"] = CountryList.ToSelectList();



并将其收集回索引页面中



and collect it back in the index page

<![CDATA[<% list = ViewData["CountryList"] as SelectList %>]]>





@Html.ListBox("Locations",list )

or

@Html.ListBox("Locations",ViewData["CountryList"] as SelectList)



另外,请在这里找到其他解决方案
http://stackoverflow.com/questions/5478936/need- help-in-html-listbox-in-asp-net-mvc [



Also, please find other solution here
http://stackoverflow.com/questions/5478936/need-help-in-html-listbox-in-asp-net-mvc[^]


这篇关于用jQuery填充模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆