Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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/2/.net/24.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
C# 使用工厂调用不同的ActionResult?_C#_.net_Asp.net Mvc - Fatal编程技术网

C# 使用工厂调用不同的ActionResult?

C# 使用工厂调用不同的ActionResult?,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,我的视图中有一个类似“所有,已读/未读,类别”的下拉列表。该视图为用户呈现通知 在控制器中,有一个默认的ActionResult显示全部,一个用于读取/未读取,另一个用于与类别相关的内容 我想使用工厂根据上面选择的下拉列表调用其中一个方法(即未读选定调用未读操作结果,原因是我不想使用switch/if语句来维护可维护的代码,我只是不确定实现。为什么不使用正确的URL作为SelectListItem的值,并使用此值重定向/加载内容 在GET用例的ViewModel中: var urlHelper

我的视图中有一个类似“所有,已读/未读,类别”的下拉列表。该视图为用户呈现通知

在控制器中,有一个默认的ActionResult显示全部,一个用于读取/未读取,另一个用于与类别相关的内容


我想使用工厂根据上面选择的下拉列表调用其中一个方法(即未读选定调用未读操作结果,原因是我不想使用switch/if语句来维护可维护的代码,我只是不确定实现。

为什么不使用正确的URL作为
SelectListItem
的值,并使用此值重定向/加载内容

在GET用例的ViewModel中:

var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

var availableOptions = new List<SelectListItem> {
    new SelectListItem {
        Text = "All",
        Value = urlHelper.Action("GetAll", "Notifications"),
        Selected = true
     },
     new SelectListItem {
        Text = "Read/Unread",
        Value = urlHelper.Action("GetReadOrUnread", "Notifications"),
        Selected = false
     },
     new SelectListItem {
        Text = "Categories",
        Value = urlHelper.Action("GetCategories", "Notifications"),
        Selected = false
     }
};
var urlHelper=newurlhelper(HttpContext.Current.Request.RequestContext);
var availableOptions=新列表{
新建SelectListItem{
Text=“全部”,
Value=urlHelper.Action(“GetAll”、“通知”),
所选=真
},
新建SelectListItem{
Text=“已读/未读”,
Value=urlHelper.Action(“GetReadOrUnread”、“通知”),
所选=错误
},
新建SelectListItem{
Text=“类别”,
Value=urlHelper.Action(“GetCategories”、“Notifications”),
所选=错误
}
};
在你看来:

@Html.DropDownListFor(m => m.SelectedView, Model.AvailableOptions)

<script>
$(document).ready(function() {
    // use @Html.IdFor so this does not break 
    // if you rename the SelectedView property in the ViewModel
    var dropdown = $('#@Html.IdFor(m => m.SelectedView)');

    dropdown.on('change', function () {
        // this gets the "value" attribute of the selected option
        var selectedUrl = $('option:selected', dropdown).val();
        $.ajax({
            url: selectedUrl, 
            success: function(result){ /* insert retrieved HTML into DOM ... */ }
        });
    });
});
</script>
@Html.DropDownListFor(m=>m.SelectedView,Model.AvailableOptions)
$(文档).ready(函数(){
//请使用@Html.idf,以免中断
//如果重命名ViewModel中的SelectedView属性
var dropdown=$(“#@Html.IdFor(m=>m.SelectedView)”;
关于('change',function()的下拉列表{
//这将获取所选选项的“value”属性
var selectedUrl=$('option:selected',下拉列表).val();
$.ajax({
url:selectedUrl,
成功:函数(结果){/*将检索到的HTML插入DOM…*/}
});
});
});

您的问题是什么?谢谢您的回答:).val()在下拉列表中获取文本值(这可能是预期的),而不是ViewModel中的value属性。这是意外的。如果您有一个
All
.val()
应返回
“/Notification/GetAll”
。text()
应返回
“All”
。我想这与如何创建SelectListItems有关。不能直接使用JavaScript访问C#模型。另请参阅