Asp.net mvc 4 如何在asp.net mvc中调用控制器方法并在视图的标签中显示详细信息

Asp.net mvc 4 如何在asp.net mvc中调用控制器方法并在视图的标签中显示详细信息,asp.net-mvc-4,Asp.net Mvc 4,我有一个带有文本框的视图,当我键入并输入服务编号时,它应该从数据库中检索数据,并在同一视图中的标签中显示这些数据,此应用程序是ASP.net MVC应用程序。谁能告诉我怎么做。谢谢 更进一步 我可以在没有javascript的情况下调用控制器方法吗 是否可以在视图中调用控制器方法并在同一视图中显示结果 如果你能告诉我怎么做,谢谢你可以使用AJAX。让我们举个例子: @Html.LabelFor(x => x.FooBar, htmlAttributes: new { id = "fooBa

我有一个带有文本框的视图,当我键入并输入服务编号时,它应该从数据库中检索数据,并在同一视图中的标签中显示这些数据,此应用程序是ASP.net MVC应用程序。谁能告诉我怎么做。谢谢

更进一步 我可以在没有javascript的情况下调用控制器方法吗

是否可以在视图中调用控制器方法并在同一视图中显示结果
如果你能告诉我怎么做,谢谢你可以使用AJAX。让我们举个例子:

@Html.LabelFor(x => x.FooBar, htmlAttributes: new { id = "fooBarLabel" })
@Html.TextBoxFor(x => x.FooBar, new { id = "fooBar", data_url = Url.Action("CalculateValue") })
然后在一个单独的javascript文件中,您可以订阅文本字段的
.change
事件,并触发对控制器操作的AJAX调用:

$(function() {
    $('#fooBar').change(function() {
        var url = $(this).data('url');
        var value = $(this).val();
        $('#fooBarLabel').load(url, { value: value });
    });
});
public ActionResult CalculateValue(string value)
{
    // The value parameter will contain the text entered by the user in the text field
    // here you could calculate the value to be shown in the label based on it:

    return Content(string.Format("You entered: {0}", value));
}
剩下的就是相应的控制器动作:

$(function() {
    $('#fooBar').change(function() {
        var url = $(this).data('url');
        var value = $(this).val();
        $('#fooBarLabel').load(url, { value: value });
    });
});
public ActionResult CalculateValue(string value)
{
    // The value parameter will contain the text entered by the user in the text field
    // here you could calculate the value to be shown in the label based on it:

    return Content(string.Format("You entered: {0}", value));
}

你好请通读帮助中心中有关提问的内容:。特别是,阅读“我如何提出一个好问题?”