我想使用 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/