Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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/17.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# 下拉选择未被清除_C#_Asp.net Mvc_Asp.net Core Mvc - Fatal编程技术网

C# 下拉选择未被清除

C# 下拉选择未被清除,c#,asp.net-mvc,asp.net-core-mvc,C#,Asp.net Mvc,Asp.net Core Mvc,我试图从下拉列表中清除所选的值,但该值仍然存在。使用@Html.DropDownListFor 控制器 public class HomeController : Controller { [Route("/Home/Index")] [Route("/Home/Index/{Category}")] [Route("/Home/Index/{Category}/{Type}")] public IActionResult Index(HomeModel mod

我试图从下拉列表中清除所选的值,但该值仍然存在。使用
@Html.DropDownListFor

控制器

public class HomeController : Controller
{

    [Route("/Home/Index")]
    [Route("/Home/Index/{Category}")]
    [Route("/Home/Index/{Category}/{Type}")]
    public IActionResult Index(HomeModel model)
    {
        // Issue is here
        // for url: home/index/accessories/test
        // "Category" is cleared if it is not valid "type"
        // but still "Accessories" remains selected in the drop down
        if (model.Type != "Electronics" && model.Type != "Furniture")
        {
            model.Category = string.Empty;
        }

        return View(new HomeModel() { Category = model.Category, Type = model.Type });
    }
查看

@model WebApplication1.Controllers.HomeModel

<select asp-for="Category" asp-items="@Model.Categories"></select>
<select asp-for="Type" asp-items="@Model.Types"></select>
@model WebApplication1.Controllers.HomeModel
型号

public class HomeModel
{
    public string Category { get; set; }

    public string Type { get; set; }

    public List<SelectListItem> Categories { get; set; } = new List<SelectListItem>
    {
        new SelectListItem() { Text="Computers & Laptops", Value="Computers-Laptops" },
        new SelectListItem() { Text="Accessories", Value="Accessories" },
    };

    public List<SelectListItem> Types { get; set; } = new List<SelectListItem>
    {
        new SelectListItem() { Text="Electronics", Value="Electronics" },
        new SelectListItem() { Text="Furniture", Value="Furniture" },
    };
}
公共类HomeModel
{
公共字符串类别{get;set;}
公共字符串类型{get;set;}
公共列表类别{get;set;}=新列表
{
新建SelectListItem(){Text=“Computers&Laptops”,Value=“Computers Laptops”},
新建SelectListItem(){Text=“附件”,Value=“附件”},
};
公共列表类型{get;set;}=新列表
{
新建SelectListItem(){Text=“Electronics”,Value=“Electronics”},
新建SelectListItem(){Text=“Furniture”,Value=“Furniture”},
};
}
更新 我试图在类别下拉列表中添加一个空值,但仍然没有成功

 <select asp-for="Category" asp-items="@Model.Categories">
        <option value="">Select Category</option>
 </select>

选择类别

问题在
ModelState
中。当URL参数绑定到视图模型时,会将这些值添加到模型状态字典中。渲染视图时,不仅会将
HomeModel
传递给视图,而且还会在场景下方传递
ModelState
,并且该视图的属性高于视图模型。因此,当您重置
model.Category
时,
ModelState
中的
Category
键仍然具有值
附件
,这就是为什么在下拉列表中选择它

解决方案很简单,请调用
.Clear()
清除模型状态并使视图模型获胜:

[Route("/Home/Index")]
[Route("/Home/Index/{Category}")]
[Route("/Home/Index/{Category}/{Type}")]
public IActionResult Index(HomeModel model) {
    // Issue is here
    // for url: home/index/accessories/test
    // "Category" is cleared if it is not valid "type"
    // but still "Accessories" remains selected in the drop down
    if (model.Type != "Electronics" && model.Type != "Furniture") {
        model.Category = string.Empty;
    }
    ModelState.Clear();
    return View(new HomeModel() { Category = model.Category, Type = model.Type });
}

问题在
ModelState
中。当URL参数绑定到视图模型时,会将这些值添加到模型状态字典中。渲染视图时,不仅会将
HomeModel
传递给视图,而且还会在场景下方传递
ModelState
,并且该视图的属性高于视图模型。因此,当您重置
model.Category
时,
ModelState
中的
Category
键仍然具有值
附件
,这就是为什么在下拉列表中选择它

解决方案很简单,请调用
.Clear()
清除模型状态并使视图模型获胜:

[Route("/Home/Index")]
[Route("/Home/Index/{Category}")]
[Route("/Home/Index/{Category}/{Type}")]
public IActionResult Index(HomeModel model) {
    // Issue is here
    // for url: home/index/accessories/test
    // "Category" is cleared if it is not valid "type"
    // but still "Accessories" remains selected in the drop down
    if (model.Type != "Electronics" && model.Type != "Furniture") {
        model.Category = string.Empty;
    }
    ModelState.Clear();
    return View(new HomeModel() { Category = model.Category, Type = model.Type });
}

选择列表中没有空值。尝试添加Text=”“选项。@KevinRaffay在添加空值后,它仍然不工作。我猜,它正在从路由中拾取值。浏览器中生成了什么html?发生错误时,您是否向该方法发布或发布了任何内容?或者只是访问“新鲜”链接?选择列表中没有空值。尝试添加Text=”“选项。@KevinRaffay在添加空值后,它仍然不工作。我猜,它正在从路由中拾取值。浏览器中生成了什么html?发生错误时,您是否向该方法发布或发布了任何内容?或者只是访问一个“新鲜”链接?