将 JQuery 创建的元素绑定到我的控制器中的参数

发布于 2024-10-12 08:18:32 字数 727 浏览 1 评论 0原文

我的视图中有一个表单,其中包含以下两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2024-10-19 08:18:33

以下内容对我来说非常适合:

模型:

public class JsonCommand
{
    public string NickName { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new[] 
        {
            new JsonCommand { NickName = "name1" },
            new JsonCommand { NickName = "name2" },
        });
    }

    [HttpPost]
    public ActionResult Index(IEnumerable<JsonCommand> command)
    {
        return Json(new { message = "ok" });
    }
}

视图(~/Views/Home/Index.aspx):

<script type="text/javascript" src="https://github.com/malsup/form/raw/master/jquery.form.js?v2.43"></script>
<script type="text/javascript">
    $('form').ajaxForm(function (result) {
        alert(result.message);
    });
</script>

<% using (Html.BeginForm()) } %>
    <%: Html.EditorForModel() %>
    <input type="submit" value="OK" />
<% } %>

编辑器模板(~/Views/Home/EditorTemplates/JsonCommand.aspx< /code>):

<%@ Control 
    Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.JsonCommand>" %>
<%: Html.RadioButtonFor(x => x.NickName, Model.NickName) %>

另请注意,单选按钮通常绑定到布尔值而不是字符串。

The following works perfectly fine for me:

Model:

public class JsonCommand
{
    public string NickName { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new[] 
        {
            new JsonCommand { NickName = "name1" },
            new JsonCommand { NickName = "name2" },
        });
    }

    [HttpPost]
    public ActionResult Index(IEnumerable<JsonCommand> command)
    {
        return Json(new { message = "ok" });
    }
}

View (~/Views/Home/Index.aspx):

<script type="text/javascript" src="https://github.com/malsup/form/raw/master/jquery.form.js?v2.43"></script>
<script type="text/javascript">
    $('form').ajaxForm(function (result) {
        alert(result.message);
    });
</script>

<% using (Html.BeginForm()) } %>
    <%: Html.EditorForModel() %>
    <input type="submit" value="OK" />
<% } %>

Editor template (~/Views/Home/EditorTemplates/JsonCommand.aspx):

<%@ Control 
    Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.JsonCommand>" %>
<%: Html.RadioButtonFor(x => x.NickName, Model.NickName) %>

Also note that radio buttons are usually bound to boolean values instead of strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文