Asp.net mvc 4 如何从ajax调用获取模型值到api控制器

Asp.net mvc 4 如何从ajax调用获取模型值到api控制器,asp.net-mvc-4,asp.net-web-api,Asp.net Mvc 4,Asp.net Web Api,我正在尝试向数据库中添加用户类型详细信息。将ajax传递到Web API时。。。模型值正在作为null传递。。。我需要将模型值作为参数传递。。有人能帮我吗 @model ConstructionModels.UserTypeModel @using Newtonsoft.Json @{ // Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="wi

我正在尝试向数据库中添加用户类型详细信息。将ajax传递到Web API时。。。模型值正在作为null传递。。。我需要将模型值作为参数传递。。有人能帮我吗

@model ConstructionModels.UserTypeModel
@using Newtonsoft.Json
@{
// Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>UserTypeModel</h4>
        <hr />


        <div class="form-group">
            @Html.LabelFor(model => model.User_Type, htmlAttributes: new {       @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.User_Type, new {  htmlAttributes = new { @class = "form-control" } })

            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" id="btnSave" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>

    var baseUrl= '@System.Configuration.ConfigurationManager.AppSettings["ApiUrl"]';


    $("#btnSave").click(function () {


      var data1 = @Html.Raw(JsonConvert.SerializeObject(this.Model));
        console.log(data1); 
        $.ajax({
            url: baseUrl + 'Usertype/Save',
            type: 'post',
            data: data1,
            dataType: 'json',

            success: function () {
                alert("Saved");
            },
            error: function (error) {
                alert("Error While Saving");
            }

        })
    })
</script>
@model ConstructionModels.UserTypeModel
@使用Newtonsoft.Json
@{
//布局=空;
}
指数
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
用户类型模型

@LabelFor(model=>model.User_类型,htmlAttributes:new{@class=“controllabel col-md-2”}) @TextBoxFor(model=>model.User_Type,new{htmlAttributes=new{@class=“form control”}}) } var baseUrl='@System.Configuration.ConfigurationManager.AppSettings[“apirl”]'; $(“#btnSave”)。单击(函数(){ var data1=@Html.Raw(JsonConvert.SerializeObject(this.Model)); console.log(data1); $.ajax({ url:baseUrl+“用户类型/保存”, 键入:“post”, 数据:数据1, 数据类型:“json”, 成功:函数(){ 警报(“已保存”); }, 错误:函数(错误){ 警报(“保存时出错”); } }) })
var data1=$('form').serialize()-您的
@Html.Raw(…)
代码只是序列化您第一次传递给视图的模型,而不是表单控件
var data1=$('form').serialize()的值-您的
@Html.Raw(…)
代码只是序列化您第一次传递给视图的模型,而不是表单控件的值