简体   繁体   English

如何在MVC3中更新没有回发的视图

[英]How to update a View without Postback in MVC3

How can I update a dropdownlist in MVC3. 如何更新MVC3中的下拉列表。 I want to refill it with latest data filled by some other view, but I do not want to postback the view and want to achieve it with jquery. 我想用其他视图填充的最新数据填充它,但我不想回发视图并希望用jquery实现它。 I have a dropdownlist like: 我有一个下拉列表,如:

@Html.DropDownListFor(m => m.Department, Model.Departments)
@Html.ValidationMessageFor(m => m.Departments)
<input type="button" value="Refresh" id="btnrefresh" />

I have written jquery code to call controller's method: 我编写了jquery代码来调用控制器的方法:

 $("#btnrefresh").click(function () {
 var ref = '@Url.Action("RefreshDepartments")';
 var model = '@Model.ToJson()';
 var data = { empModel: model };
 $.getJSON(ref, data, function (result) { alert(result.message); });
        return false;
    });

And Here is the controller method: 这是控制器方法:

public ActionResult RefreshDepartments(EmployeeModel empModel)
    {
        empModel.Departments = GetDepartments();
        empModel.Roles = GetRoles();
        return Json(new { message = "Updated successfully"}, JsonRequestBehavior.AllowGet);
    }

How can I update the dropdownlist with latest values on clicking "Refresh" button without any postback? 如何在没有任何回发的情况下点击“刷新”按钮更新带有最新值的下拉列表? Is it a good idea to pass the model to the controller and update the model properties? 将模型传递给控制器​​并更新模型属性是一个好主意吗? What other ways are possible ? 还有什么方法可以吗?

It doesn't look to me like you need the model to be posted to your controller for what you're doing. 它并不像我一样需要将模型发布到您的控制器上以实现您的目的。 In addition, yes, you absolutely can do this with jquery! 另外,是的,你绝对可以用jquery做到这一点! On a side note, you could also do it with an Ajax.BeginForm() helper method, but lets deal with your jquery example. 另外,您也可以使用Ajax.BeginForm()辅助方法来完成它,但是让我们来处理您的jquery示例。

Rather than complexify your jquery with your @Url.Action, you can simply call the path itself. 您可以简单地调用路径本身,而不是将您的jquery与@ Url.Action复杂化。

$("#btnrefresh").click(function () {
    var ref = 'ControllerName/RefreshDepartments';
    $.each(result, function (index, val) {
        $('#whateverYourRenderedDropdownListHtmlObjectis')
            .append($("<option></option>")
            .attr("value", val.Text)
            .text(val.Text));
    });
});

Now, for your controller... 现在,为您的控制器......

public JsonResult RefreshDepartments()
{
    return Json(GetDepartments, JsonRequestBehavior.AllowGet);
}

private SelectList GetDepartments
{
    var deparments = GetDepartments;
    SelectList list = new SelectList(departments);
    return list;
}

This is an alternative to returning the model. 这是返回模型的替代方法。 It allows you to manipulate the raw JSON instead. 它允许您操纵原始JSON。 Hope it helps! 希望能帮助到你!

You almost did it all! 你几乎做到了! Why don't you send the data, I mean list, by RefreshDepartments action? 你为什么不通过RefreshDepartments动作发送数据,我的意思是列表? You sent a message to view, so you can send the list similarly and instead of alerting the result you can fill the dropdownlist. 您发送了一条消息进行查看,因此您可以类似地发送列表,而不是警告结果,您可以填写下拉列表。 something like this: 这样的事情:

public ActionResult RefreshDepartments(EmployeeModel empModel)
    {
        return Json(new { departments = GetDepartments()}, JsonRequestBehavior.AllowGet);
    }


$.getJSON(ref, data, function (result) { 
    $("#Department").html("");
    for (var i = 0; i < result.departments.length; i++) {
        var item = result.departments[i];
        $("#Department").append(
              $("<option></option>").val(item.Id).html(item.Name);
        );
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM