您好我有一个问题,即本地主机后使用弹出窗口在数据库中没有保存数据。我会清楚地解释我的问题。我有一个名为客户的观点。在那里,我有一个名为Area的字段,并且我在该字段附近保留一个添加按钮。
区域
当我点击+按钮时,它将打开弹出窗口。弹出窗口在下面提到
区域流行winodw
当我构建并运行应用程序并打开此视图并单击+按钮并输入所有详细信息并单击保存按钮时,它将值保存在db中。一切正常。但在我发布我的applciation并打开此视图并单击+按钮后,它会打开弹出窗口。直到它的工作正常。但是当我输入所有细节并单击保存按钮时,它不会将数据保存在Db中。这是问题所在。发布我的应用程序后,数据不会使用弹出窗口保存在Db中。但是在正常运行中它完全正常工作。
发布弹出窗口后
它正在努力 $( '#areaID表示')附加($( '')VAL(数据)的.text(区域)。);
但是db中没有保存。但是在正常运行中一切正常,使用弹出窗口的savinf数据在正常的应用程序运行中完全正常工作。但是当我在本地主机上托管我的应用程序后,spopup窗口无法正常工作。
我的观看代码
<div id="Area">
<div class="col-sm-3">
<span style="color: #f00">*</span>
@Html.LabelFor(model => model.Area, new { @class = "control-label" })<a href="#" class="btn btn-primary" id="modal-opener" style="float: right; Width: 28px; height: 28px; ">+</a>
<div id="dialog-modal" title="Add New Area" style="display:none">
<div class="box-body">
<span class="red">*</span>
@Html.Label("Area", new { @class = "control-label" })
@Html.TextBoxFor(model => model.Area, new { @class = "form-control", type = "text", id = "AreaName" })
@Html.ValidationMessageFor(model => model.Area)
@Html.Label("City")
@Html.DropDownListFor(model => model.CityID, new SelectList(string.Empty, "Value", "Text"), "Please select a City", new { @class = "form-control required", type = "text", id = "City" })
<div class="box-footer">
<input id="Button1" class="btn btn-primary" type="button" value="Save" onclick="SaveArea()" />
</div>
</div>
</div>
@Html.DropDownList("AreaID", null, "Select", new { @class = "form-control required" })
</div>
</div>
我的J查询代码
<script type="text/javascript">
$(function () {
debugger
$("#dialog-modal").dialog({
resizable: false,
width: 500,
maxHeight: 400,
draggable: true,
dialogClass: 'main-dialog-class',
autoOpen: false,
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "explode",
duration: 1000
},
open: function () {
var closeBtn = $('.ui-dialog-titlebar-close');
closeBtn.append('<span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span>');
},
close: function () {
var closeBtn = $('.ui-dialog-titlebar-close');
closeBtn.html('');
},
});
$("#modal-opener").click(function () {
debugger
$("#dialog-modal").dialog("open");
});
});
function onSuccess() {
$("#dialog-modal").dialog("close");
}
function SaveArea() {
debugger
var url = '@Url.Action("Customer", "AddAreaInfo", new { area = "Sales" })';
var customerContact = $('#modalform').serialize(); // must have a <form id="modalform"> in the popup
var Area = $("#AreaName").val();
var CityID = $("#City").val();
var AreaAdd = { "Area": '' + Area + '', "CityID": CityID };
$.post("/Sales/Customer/AddAreaInfo", AreaAdd, function (data) {
$('#AreaID').append($('<option><option>').val(data).text(Area));
$('#AreaID').val(data);
alert ("Success")
});
}
$(function () {
$.ajax(
'@Url.Action("GetCity", "Customer", new { Area = "Sales" })',{
type: "GET",
datatype: "Json",
success: function (data) {
$.each(data, function (index, value) {
$('#City').append('<option value="' + value.CityID + '">' +value.DisplayName + '</option>');
});
}
});
});
控制器
public JsonResult AddAreaInfo(CustomerViewModel objareaVM)
{
var objAreaID = Guid.NewGuid();
ViewBag.CityID = new SelectList(db.Cities, "CityID", "DisplayName", objareaVM.CityID);
var ObjArea = new Area()
{
AreaID = objAreaID,
DisplayName = objareaVM.Area,
PrintName = objareaVM.Area,
CityID = objareaVM.CityID,
IsActive = true,
IsDeleted = false,
CreatedDate = DateTime.Now,
EditedDate = DateTime.Now,
LastActiveOn = DateTime.Now,
RowID = Guid.NewGuid(),
CreatedSessionID = Guid.NewGuid(),
EditedSessionID = Guid.NewGuid(),
OfflineMode = false,
OfflineID = Guid.NewGuid()
};
db.Areas.Add(ObjArea);
db.SaveChanges();
ModelState.Clear();
return Json(objAreaID);
}
public JsonResult GetCity()
{
return Json(db.Cities.ToList(), JsonRequestBehavior.AllowGet);
}
答案 0 :(得分:0)
我自己清楚我的问题。我想分享我的答案。为什么它不在db中保存是j-query中控制器的路径不正确。
最后我更正了我的j-query代码
答案是
J查询代码
function SaveArea() {
debugger
var url = '@Url.Action("AddAreaInfo", "Customer", new { area = "Sales" })';
var customerContact = $('#modalform').serialize(); // must have a <form id="modalform"> in the popup
var Area = $("#AreaName").val();
var CityID = $("#City").val();
var AreaAdd = { "Area": '' + Area + '', "CityID": CityID };
$.post(url, AreaAdd, function (data) {
$('#AreaID').append($('<option><option>').val(data).text(Area));
$('#AreaID').val(data);
alert ("Success")
});
}
在我开始之前
var url = '@Url.Action(" Customer", "AddAreaInfo", new { area = "Sales" })';
现在我将其更改为
var url = '@Url.Action("AddAreaInfo", "Customer", new { area = "Sales"})';
因此问题得到解决,最后将数据保存在数据库中。