如何从ajax调用获取模型值到api控制器

时间:2017-02-21 09:47:31

标签: asp.net-mvc-4 asp.net-web-api

我正在尝试将用户类型详细信息添加到数据库。当通过ajax传递给Web API时...模型值传递为null ...我需要将Model值作为参数传递..可以帮助我这个

@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>

0 个答案:

没有答案