将 JQuery 创建的元素绑定到我的控制器中的参数
我的视图中有一个表单,其中包含以下两个 html 元素(这些元素是由 JQuery 动态创建的,未使用模型绑定到视图)。
<input id="JsonCommand_0__NickName" type"radio" name="JsonCommand[0].NickName" value="name1" checked>
<input id="JsonCommand_1__NickName" type"radio" name="JsonCommand[1].NickName" value="name2" checked>
我有以下课程:
public class JsonCommand
{
public string NickName { get; set; }
}
我有以下控制器:
[HttpPost, Authorize, Compress]
public ActionResult Edit(IEnumerable<JsonCommand> command)
{
....
}
我正在使用 JQuery.Form 插件发布到此控制器。我是否可以通过这种方式将表单集合序列化为 JsonCommand 对象?目前,当我尝试时,我得到命令的空值。
有什么方法可以在客户端创建一个集合并将其绑定到我的 JsonCommand 对象吗?
谢谢
I have a form in my view with the following two html elements (these elements were created dynamically by JQuery and were not bound to the view using the Model).
<input id="JsonCommand_0__NickName" type"radio" name="JsonCommand[0].NickName" value="name1" checked>
<input id="JsonCommand_1__NickName" type"radio" name="JsonCommand[1].NickName" value="name2" checked>
I have the following class:
public class JsonCommand
{
public string NickName { get; set; }
}
I have the following controller:
[HttpPost, Authorize, Compress]
public ActionResult Edit(IEnumerable<JsonCommand> command)
{
....
}
I am using JQuery.Form plugin to post to this controller. Is it possible for me to serialize the form collection into a JsonCommand objects this way? Currently when I try I get a null value for command.
Is there any way I can create a collection on the client side and bind it to my JsonCommand object?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下内容对我来说非常适合:
模型:
控制器:
视图(
~/Views/Home/Index.aspx
):编辑器模板(
~/Views/Home/EditorTemplates/JsonCommand.aspx< /code>):
另请注意,单选按钮通常绑定到布尔值而不是字符串。
The following works perfectly fine for me:
Model:
Controller:
View (
~/Views/Home/Index.aspx
):Editor template (
~/Views/Home/EditorTemplates/JsonCommand.aspx
):Also note that radio buttons are usually bound to boolean values instead of strings.