当我选择下拉列表并单击编辑按钮时,我有一个下拉列表,它应该打开部分视图,其中的值从textboxfor的下拉列表中选择。但是当我第一次打开包含下拉列表的页面时它工作得很完美,但是当我关闭局部视图而不在局部视图中执行任何操作时,当我选择下拉列表值并单击编辑按钮时,它会获取先前选择的值而不是最新的值。 以下是我的代码。
View With Ajax call to controller. $(document).ready(function () {
if ('@TempData["message"]' == "updated") {
toastr.success('Updated Successfuly');
}
else if ('@TempData["message"]' == "already") {
toastr.error('Domain Already Exists');
}
else if ('@TempData["message"]' == "deleted") {
toastr.success('Deleted Successfuly');
}
});
function del() {
var url = "/UserGroupEdit/Delete";
var id = $(this).attr('data-id');
$.post(url + '/' + id, function (data) {
$('#ModalPopupContent').html(data);
$('#myModal').modal('show');
});
}
function dd(mode) {
if (mode == "Edit") {
var a = $("#Domain").val();
if (a != "") {
var url = "../DomainCRUD/EditDom";
var id = $("#Domain").val();
var obj = {
InstructionHeader: 'Edit',
Domain: id
}
alert(a);
try {
$.ajax({
url: "../DomainCRUD/EditDom",
data: obj,
async: false,
method: "POST",
success: function (response) {
$('#ModalPopupContent').html(response);
$('#myModal').modal('show');
}
});
} catch (e) {
}
}
else {
}
}
else if (mode == "Delete") {
var a = $("#Domain").val();
if (a != "") {
var action = confirm("Are you sure you want to Delete");
if (action) {
var url = "../DomainCRUD/EditDom";
var id = $("#Domain").val();
var obj = {
InstructionHeader: 'Delete',
Domain: id
}
try {
$.ajax({
url: "../DomainCRUD/EditDom",
data: obj,
async: false,
method: "POST",
success: function (response) {
if (response == "exist") {
toastr.error('You cannot Delete this Domain as it is alloted to some users.');
}
else
toastr.success('Deleted Successfuly');
}
});
} catch (e) {
}
}
else {
}
}
else {
}
}
}
</script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="content-box">
<div class="page-title">
<h2>Add New Domain</h2>
</div>
<div class="create-button">
@Html.ActionLink("Create New", "Create")
</div>
<div class="content-box dashbord-content-box">
<div class="label-row">
<div class="label-cont">
<label for="exampleInputEmail1">List of Domains</label>
</div>
<div class="label-box">
@Html.DropDownListFor(model => model.Domain, ViewBag.GroupType as IEnumerable<SelectListItem>, "--Select--", new { required = "required", oninvalid = "this.setCustomValidity('Please select Domain')", oninput = "setCustomValidity('')" })
</div>
</div>
<div class="button-box">
<div class="label-cont">
<label for="exampleInputEmail1"></label>
</div>
<input type="button" name="submit" value="Edit" onclick="dd('Edit');" class="btn btn-default sub-but" />
<input type="button" name="submit" value="Delete" onclick="dd('Delete');" class="btn btn-default sub-but" />
</div>
</div>
</div>
}
我的控制器行动。
public ActionResult Index()
{
if (Session["Email"] != null && Session["GroupType"].ToString() == "1")
{
var res = (from u in db.ConfigurationSettings select u.Domain).FirstOrDefault();
string[] thisArray = res.Split(',');
List<string> myList = new List<string>();
myList.AddRange(thisArray);
ViewBag.GroupType = new SelectList(myList);
return View();
}
else
return RedirectToAction("LogIn", "Auth");
}
[HttpPost]
public ActionResult EditDom(ConfigurationSetting config)
{
if (ModelState.IsValid)
{
if (config.Domain != null)
{
if (config.InstructionHeader == "Edit")
{
Session["editeddomain"] = config.Domain;
return PartialView("EditDompartial", config);
}
else
{
var isexist = (from u in db.Users where u.DomainName == config.Domain select u).Any();
if (isexist == false)
{
Session["editeddomain"] = config.Domain;
DeleteDomain();
TempData["message"] = "deleted";
return JavaScript("location.reload(true)");
}
else
{
TempData["message"] = "failed";
return Content("exist");
}
}
}
else
return RedirectToAction("Index", "DomainCRUD");
}
else
return RedirectToAction("Index", "DomainCRUD");
}
部分视图
<div class="form-group">
@Html.LabelFor(model => model.Domain, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="login_field">
@Html.TextBoxFor(model => model.Domain, new { required = "required", htmlAttributes = new { } })
@Html.ValidationMessageFor(model => model.Domain, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-sm-4"> </div>
<input type="submit" value="Save" formaction="SaveEditedDomain" class="btn btn-default save-button" />
</div>
</div>