我试图将视图中的MVC模型对象传递给Controller Action方法,以访问我的数据库.....我打算将该元素添加到数据库中..... ....但我不确定如何将模型对象转换为jQuery变量.....我尝试了两种方法来实现.....
var modelDataJSON = @Model;
var modelDataJSON = @Html.Raw(Json.Encode(Model));
这两种方式都给我带来语法错误.....
这是我的jQuery函数的样子:
$('#submit').click(function () {
var modelDataJSON = @Model;
@*var modelDataJSON = @Html.Raw(Json.Encode(Model));*@
$.ajax({
type: "POST",
url: '@Url.Action("AddDoctor")',
data: {"doctor" : modelDataJSON},
success: function (data) {
alert(JSON.stringify(data.Message));
},
error: function () {
alert("Error!!!");
}
});
return false;
});
这是我的表格的样子:
@using (Html.BeginForm("AddDoctor", "Doctor"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4></h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@*@Html.LabelFor(model => model.UserId, new { @class = "col-sm-2 control-label" })*@
<label class="col-sm-2 control-label"> User ID</label>
<div class="col-md-10">
@Html.TextBoxFor(model => model.UserId, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "col-sm-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.DoctorSpecialityId, new { @class = "col-sm-2 control-label" })*@
<label class="col-sm-2 control-label"> Doctor Speciality ID</label>
<div class="col-md-10">
@Html.TextBoxFor(model => model.DoctorSpecialityId, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DoctorSpecialityId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Charges, new { @class = "col-sm-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Charges, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Charges)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhoneNo, new { @class = "col-sm-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.PhoneNo, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PhoneNo)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.WardId, new { @class = "col-sm-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.WardId, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.WardId)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-primary" id="submit" />
</div>
</div>
最佳答案
您必须将其包装在引号中以使其成为字符串: