使用jquery将模型传递给action

时间:2014-08-07 10:08:17

标签: jquery asp.net-mvc-4

我想使用jquery

将Model传递给控制器​​操作

这是我的控制器操作代码

[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});
    });

我该怎么做?

1 个答案:

答案 0 :(得分:3)

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

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