jquery - 使用 jquery 将模型传递给操作

标签 jquery asp.net-mvc-4

我想使用 jquery 将模型传递给 Controller ​​操作

这是我的 Controller 操作代码

[HttpPost]
        public PartialViewResult AddUserStatus(NewBlogPostModel model)
        {
            //code goes here
        }

这是我的查看代码

@model Platform3.Models.ViewModels.NewBlogPostModel

<div class="pull-left">
    <div class="editor-field" >        
        <textarea class="form-control" style="resize: none;" rows="4" cols="80" maxlength="200" id="user-status" placeholder="What's in your mind?"></textarea>
        <div id="textarea_feedback"></div>
    </div>
    <div class="pull-right">
        <input type="submit" id="submit-button" value="Update Status" class="btn btn-primary" style="margin-top: -15px;">
    </div>
</div>
$("#submit-button").click(function () {        
            var status = $('#user-status').val();
            // I want to assign status to Model.Body;
            // Call Create action method
            // I want to pass the model to the action here
            $.post('/Partial/AddUserStatus', { model:  Model});
    });

我该怎么做?

最佳答案

您可以使用Json.Encode方法将数据对象转换为 JavaScript 对象表示法 (JSON) 格式的字符串。

var model = @Html.Raw(Json.Encode(Model)); //Convert Model to JSON 
$.post('/Partial/AddUserStatus', model);

关于jquery - 使用 jquery 将模型传递给操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25179726/

相关文章:

c# - 自定义表面 Controller Umbraco 的问题

asp.net-mvc - asp mvc 使用 View 模型在 View 中列出产品详细信息

asp.net-mvc-4 - 绑定(bind)可变数量的输入

jquery - 使用完整日历突出显示所有月份的特定日期

asp.net - SignalR - 不使用长轮询调用 OnReconnected

javascript - 单击除自身之外的任何其他元素时隐藏视频

jquery - 选择 html 元素内的文本并更改样式

c# - 不可调用成员 'File' 在生成报告时不能像方法一样使用

javascript - 根据定向链接预填充重力形式下拉列表

javascript - jQuery 创建和修改元素