Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从jquery脚本查看调用后不加载操作控制器_Jquery_Asp.net Mvc - Fatal编程技术网

从jquery脚本查看调用后不加载操作控制器

从jquery脚本查看调用后不加载操作控制器,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我有以下问题: 交换DropDownList的值后,使用脚本在控制器中调用Create操作,模型将正确更新,但未在视图中显示更改。不在视图中显示任何更改,尽管模型已更改,但仍保持不变。 请告诉我哪里错了。 多谢各位 控制器: public ActionResult Create(int? id) { IEnumerable<Instructor> selIns = db.Instructors.Where(x => x.ID =

我有以下问题: 交换DropDownList的值后,使用脚本在控制器中调用Create操作,模型将正确更新,但未在视图中显示更改。不在视图中显示任何更改,尽管模型已更改,但仍保持不变。 请告诉我哪里错了。 多谢各位

控制器:

 public ActionResult Create(int? id)
    {            
        IEnumerable<Instructor> selIns = db.Instructors.Where(x => x.ID == id);

        var model = new CreateDepartmentViewModel
        {
            Department = new Department(),                
            Instructors = selIns,
        };
        return View(model);
    }    
公共操作结果创建(int?id) { IEnumerable selIns=db.Instructors.Where(x=>x.ID==ID); var模型=新建CreateDepartmentViewModel { 部门=新部门(), 讲师=selIns, }; 返回视图(模型); } 视图:

@if(Model.Instructors!=null)
{
教官
名称
等级
@foreach(Model.Instructors中的var项)
{
@item.FullName
@DisplayFor(modeleItem=>item.HireDate)
}
}
….
@Html.DropDownListFor(x=>x.Department.InstructorID,Model.InstructorID,htmlAttributes:new{@id=“intuctors”,@class=“form control”})
…..
$(文档).ready(函数(){
$(“#指令”).change(函数(){
var idselect=$(“#内部结构”).val();
$.get(“/Department/Create”,{id:idselect},函数(数据){
});
});
});

首先,您没有对从
Create()
方法返回的视图执行任何操作。你的脚本需要修改

$("#intructors").change(function () {
  var idselect = $("#intructors").val();
  $.get("/Department/Create", { id: idselect }, function (data) {
    $(someElement).html(data);
  });
});
其中
someElement
是一个html元素,您希望在其中插入返回的html。此外,您的方法需要返回
PartialView(model),而不是
视图(模型)

从注释中,您需要一个附加的方法和局部视图,如下所示

控制器

// The main method for additional generating you view
public ActionResult Create(int? id)
{            
  ....
  return View(model);
}

// The method that is called by your ajax call
public ActionResult FetchInstructors(int id)
{
  // note the following line would fail if parameter id is null so it should be int id, not int? id
  IEnumerable<Instructor> model = db.Instructors.Where(x => x.ID == id);     
  return PartialView("_Instructors", model);
}

您没有对
Create()
方法返回的数据执行任何操作。您希望看到什么?您能显示完整视图代码吗?我有一个模型属性(命名的讲师),当我交换DropDownList值时,该属性会更新,并且我想显示那些选择了ID的讲师。控制器中的模型正在正确更改,但视图中未显示更改。@Turdanu,同样-您没有对返回给客户机的视图执行任何操作(在任何情况下,它都需要是部分视图)。您想对它做什么-即使用
id=“someValue”
?@Stephen Mueke将其添加到现有div元素内的DOM中。谢谢你的回答。我添加了所有重要的代码。$。获取(“/Department/Create”,{id:idselect}…在控制器模型中使用选定的讲师id调用创建操作。讲师已更新,但视图未显示更新的模型。讲师。Stephen,非常感谢您的帮助。这正是我想要做的。再次感谢您。
// The main method for additional generating you view
public ActionResult Create(int? id)
{            
  ....
  return View(model);
}

// The method that is called by your ajax call
public ActionResult FetchInstructors(int id)
{
  // note the following line would fail if parameter id is null so it should be int id, not int? id
  IEnumerable<Instructor> model = db.Instructors.Where(x => x.ID == id);     
  return PartialView("_Instructors", model);
}
@model CreateDepartmentViewModel
....
<h3>Instructors</h3>
<div id="Instructors">
  @Html.Partial("_Instructors", Model.Instructors)
</div>
.... // your dropdownlist etc.
@model IEnumerable<Instructor>
<table>
  <thead>
    ....
  </thead>
  <tbody>
    @foreach (var item in Model)
    {
      <tr>
        <td>@item.FullName</td>
        <td>@Html.DisplayFor(m => item.HireDate)</td>
      </tr>
    }
  </tbody>
</table>
var placeholder = $('#Instructors'); // cache it
var url = '@Url.Action("FetchInstructors", "Department")'; // don't hard code you url's
$("#intructors").change(function () {
  var idselect = $(this).val(); // use $(this) so you don't traverse the DOM again
  $.get(url, { id: idselect }, function (data) {
    placeholder.html(data);
  });
});