Asp.net mvc MVC Html下拉菜单使用razor更改表的内容

Asp.net mvc MVC Html下拉菜单使用razor更改表的内容,asp.net-mvc,razor,onchange,html-select,asp.net-mvc-partialview,Asp.net Mvc,Razor,Onchange,Html Select,Asp.net Mvc Partialview,嗨,有人能帮我做以下事情吗。 在(PartialView)Dropdownlist中更改值时,我只需更改表的内容就可以了 控制器 public ActionResult Index(int id = 0) { var testlines = db.testlines.Include(t => t.Test) .Where(t => t.TestId == id) .Select(t => new TestLineList {

嗨,有人能帮我做以下事情吗。 在(PartialView)Dropdownlist中更改值时,我只需更改表的内容就可以了

控制器

public ActionResult Index(int id = 0)
    {
       var testlines = db.testlines.Include(t => t.Test)
        .Where(t => t.TestId == id)
        .Select(t => new TestLineList { 
        Id = t.id,
        TestId = id,
        TextBefore = t.TextBefore,
        TextAfter = t.TextAfter,
        TestName = t.Test.Name,
        Answer = t.Answer,
        AudioPath = t.AudioPath
        });

           var items = db.tests.ToList().Select(i => new SelectListItem() { Value = i.Id.ToString() , Text = i.Name });
           List<SelectListItem> testlist = new List<SelectListItem>();
           testlist.AddRange(items);

           testlist.Insert(0, new SelectListItem() { Value = "0", Text = "-- Vælg Test --" });
           SelectList sl = new SelectList(testlist, "Value", "Text", id);
       ViewBag.TestId = sl;

       if (Request.IsAjaxRequest())
       {
           return PartialView("PartialTestlines", testlines);
       }

       return View(testlines.ToList());
    }
    [HttpPost]
    public ActionResult PartialTestlines(int? id)
    {
        if (!id.HasValue) { id = 0; }
        var testlines = db.testlines.Include(t => t.Test)
         .Where(t => t.TestId == id)
         .Select(t => new TestLineList
         {
             Id = t.id,
             TestId = id.HasValue ? (int)0:(int)id,
             TextBefore = t.TextBefore,
             TextAfter = t.TextAfter,
             TestName = t.Test.Name,
             Answer = t.Answer,
             AudioPath = t.AudioPath
         });

        var items = db.tests.ToList().Select(i => new SelectListItem() { Value = i.Id.ToString(), Text = i.Name });
        List<SelectListItem> testItems = new List<SelectListItem>();
        testItems.AddRange(items);


        testItems.Insert(0, new SelectListItem() { Value = "0", Text = "-- Vælg Test --" });
        SelectList sl = new SelectList(testItems, "Value", "Text", id);
        ViewBag.TestId = sl;

        if(Request.IsAjaxRequest()) 
        {
            //return this.Json(testlines, JsonRequestBehavior.AllowGet);
            return PartialView("PartialTestlines",testlines);
        }
        return View(testlines.ToList());

    }`enter code here`
onchange函数不发出ajax请求,也无法从dropdownlist获取值并将其用作参数ID

我发现我可以通过调用这个脚本来执行ActionResult,因为表的内容没有被替换,所以您在脚本上遇到了错误。有什么想法吗

jQuery(document).ready(function () {
$("#Testdrop").change(function () {
    var tempid = $("#Testdrop").val();
    $.ajax({
        url: "/TestLine/PartialTestlines",
        type : "post",
        data: {"id" : tempid } ,// Send value of the drop down change of option
        dataType: "json", // Choosing a JSON datatype
        success: function (data) {

            // Variable data contains the data you get from the action method
        },
        error: function (e) { alert(e.error);}
    });
});
}))

这会奏效的。为什么我不能用下拉菜单做同样的事情

@using(Ajax.BeginForm("Index", "TestLine", new AjaxOptions { UpdateTargetId = "highlight" }))
{  

// @Html.DropDownList("Testdrop", (SelectList)ViewBag.TestId,((SelectList)iewBag.TestId).SelectedValue)         

     <input type="search" name="id" />
<input type="submit" value="id" />   
}      
@使用(Ajax.BeginForm(“Index”、“TestLine”、新的AjaxOptions{UpdateTargetId=“highlight”}))
{  
//@Html.DropDownList(“Testdrop”,(SelectList)ViewBag.TestId,((SelectList)iewBag.TestId).SelectedValue)
}      

脚本是否命中了您的“PartialTestlines()”?你调试过它吗?是的,它使用下拉列表中的参数运行,并重新编写PartialView PartialTestlines。但随后它遇到了Ajax调用的错误函数。我不知道我是如何得到这个错误的,这样我就可以看到哪里出了问题。这是因为您在返回视图和ajax调用时的操作期望它的数据类型是json。因此,它会跳转到错误块。尝试返回json数据,而不是返回
ActionResult
。或者你可以以纯字符串的形式返回视图。嗨,Jatin,什么都没有发生,甚至没有调用error函数,我不明白的是,如果我插入一个文本输入框和一个submit按钮并调用index操作,一切都会正常工作。我也会做同样的事情,只是在下拉列表中更改索引。看看这篇文章:
jQuery(document).ready(function () {
$("#Testdrop").change(function () {
    var tempid = $("#Testdrop").val();
    $.ajax({
        url: "/TestLine/PartialTestlines",
        type : "post",
        data: {"id" : tempid } ,// Send value of the drop down change of option
        dataType: "json", // Choosing a JSON datatype
        success: function (data) {

            // Variable data contains the data you get from the action method
        },
        error: function (e) { alert(e.error);}
    });
});
@using(Ajax.BeginForm("Index", "TestLine", new AjaxOptions { UpdateTargetId = "highlight" }))
{  

// @Html.DropDownList("Testdrop", (SelectList)ViewBag.TestId,((SelectList)iewBag.TestId).SelectedValue)         

     <input type="search" name="id" />
<input type="submit" value="id" />   
}