C# 如何修复冲突';索引()';行动结果

C# 如何修复冲突';索引()';行动结果,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,在我的视图(Index.cshtml)中,我有一个复选框列表和一个文件输入表单,两者不能一起工作,因为两者都使用Index(),并且在一个控制器中不能有多个Index(),那么我该怎么办?创建另一个视图?我不想创建另一个视图,因为我希望复选框和文件输入都在同一个视图上,那么我该怎么办 复选框: [HttpGet] public ActionResult Index() //Index being used { var list = new List<Album> {

在我的视图(Index.cshtml)中,我有一个复选框列表和一个文件输入表单,两者不能一起工作,因为两者都使用
Index()
,并且在一个控制器中不能有多个
Index()
,那么我该怎么办?创建另一个视图?我不想创建另一个视图,因为我希望复选框和文件输入都在同一个视图上,那么我该怎么办

复选框:

[HttpGet]
public ActionResult Index() //Index being used
{
    var list = new List<Album>
    {
        new Album { Id = 1, Name = "Aquafina", Checked = false },
        new Album { Id = 2, Name = "Mulshi Springs", Checked = false },
        new Album { Id = 3, Name = "Alfa Blue", Checked = false },
        new Album { Id = 4, Name = "Atlas Premium", Checked = false },
        new Album { Id = 5, Name = "Bailley", Checked = false },
        new Album { Id = 6, Name = "Bisleri", Checked = false },
        new Album { Id = 7, Name = "Himalayan", Checked = false },
        new Album { Id = 8, Name = "Cool Valley", Checked = false },
        new Album { Id = 9, Name = "Dew Drops", Checked = false },
        new Album { Id = 10, Name = "Dislaren", Checked = false },
    };
    return View("Index", list);
}

[HttpPost]
public ActionResult Index(List<Album> list) //2nd Index being used, so far so good
{
    var selected = list.Where(x => x.Checked).Select(x => x.Name);
    //ViewBag.Values = String.Join(", ", list);
    ViewBag.Values = selected;

    return this.View("Index", list);
}
可以使用该属性为第二个索引方法设置索引别名,然后重命名该方法

可以使用该属性为第二个索引方法设置索引别名,然后重命名该方法

  • 第一个索引之所以有效,是因为它是[HttpGet]
  • 第二个索引之所以有效,是因为它是[HttpPost]
  • 您不能在该控制器中定义另一个名为Index for HttpGet或HttpPost的操作,因为当您调用该操作时,MVC如何知道要调用哪个索引

    只需为其他两个操作指定唯一名称:

    public ActionResult BindCombobox()
    {
    
    }
    
    [HttpPost]
    public ActionResult UploadFiles(HttpPostedFileBase file)
    {
    
    }
    
  • 第一个索引之所以有效,是因为它是[HttpGet]
  • 第二个索引之所以有效,是因为它是[HttpPost]
  • 您不能在该控制器中定义另一个名为Index for HttpGet或HttpPost的操作,因为当您调用该操作时,MVC如何知道要调用哪个索引

    只需为其他两个操作指定唯一名称:

    public ActionResult BindCombobox()
    {
    
    }
    
    [HttpPost]
    public ActionResult UploadFiles(HttpPostedFileBase file)
    {
    
    }
    

    在视图中,定义要上传文件的表单操作,此操作的名称由您决定

    Index.cshtml

    @Html.BeginForm("UploadFile", "Controller", new { type = "form/multipart" })
    {
        <input type="file" name="file" />
        <button type="submit" value="Upload" />
    }
    
    @Html.BeginForm("Index", "Controller")
    {
        <input type="checkbox" name="Album" value="No. Dolls!" />
        <input type="checkbox" name="Album" value="O.B.I." />
        <button type="submit" value="Upload" />
    }
    
    @Html.BeginForm(“上传文件”,“控制器”,新的{type=“form/multipart”})
    {
    }
    @BeginForm(“索引”、“控制器”)
    {
    }
    
    和Controller.cs

    [HttpPost]
    public ActionResult UploadFile(HttpPostedFileBase file) 
    { 
        // here you are working with uploaded file
        return View("Index"); 
    }
    
    [HttpPost]
    public ActionResult Index(List<Album> list)
    {
        // here you are working with checkbox list items
        return View();
    }
    
    [HttpPost]
    公共操作结果上载文件(HttpPostedFileBase文件)
    { 
    //这里您正在使用上载的文件
    返回视图(“索引”);
    }
    [HttpPost]
    公共行动结果索引(列表)
    {
    //这里您正在使用复选框列表项
    返回视图();
    }
    
    一些离题注释:使用接口类instate具体实现
    List
    =>
    IList
    ;IList支持添加相册,将相册添加到上传的收藏中有意义吗

    如果不是,我建议使用
    IEnumerable
    ,但使用IEnumerable是一个问题:您可以修改元素,并且代码的某些部分不需要更改(例如设置null值),这可能导致null引用异常。如果你对此没问题,就用它


    如果没有,我的建议是在视图中使用IReadOnlyCollection定义表单操作以上传文件,此操作的名称由您决定

    Index.cshtml

    @Html.BeginForm("UploadFile", "Controller", new { type = "form/multipart" })
    {
        <input type="file" name="file" />
        <button type="submit" value="Upload" />
    }
    
    @Html.BeginForm("Index", "Controller")
    {
        <input type="checkbox" name="Album" value="No. Dolls!" />
        <input type="checkbox" name="Album" value="O.B.I." />
        <button type="submit" value="Upload" />
    }
    
    @Html.BeginForm(“上传文件”,“控制器”,新的{type=“form/multipart”})
    {
    }
    @BeginForm(“索引”、“控制器”)
    {
    }
    
    和Controller.cs

    [HttpPost]
    public ActionResult UploadFile(HttpPostedFileBase file) 
    { 
        // here you are working with uploaded file
        return View("Index"); 
    }
    
    [HttpPost]
    public ActionResult Index(List<Album> list)
    {
        // here you are working with checkbox list items
        return View();
    }
    
    [HttpPost]
    公共操作结果上载文件(HttpPostedFileBase文件)
    { 
    //这里您正在使用上载的文件
    返回视图(“索引”);
    }
    [HttpPost]
    公共行动结果索引(列表)
    {
    //这里您正在使用复选框列表项
    返回视图();
    }
    
    一些离题注释:使用接口类instate具体实现
    List
    =>
    IList
    ;IList支持添加相册,将相册添加到上传的收藏中有意义吗

    如果不是,我建议使用
    IEnumerable
    ,但使用IEnumerable是一个问题:您可以修改元素,并且代码的某些部分不需要更改(例如设置null值),这可能导致null引用异常。如果你对此没问题,就用它


    如果没有,我的建议是使用IReadOnlyCollection

    像Ton Plooij建议的那样,为控制器函数使用不同的方法名

    [HttpGet, ActionName("Index")]
    public ActionResult GetIndex()
    {
        ...
    }
    
    [HttpPost, ActionName("Index")]
    public ActionResult PostIndex()
    {
        ...
    }
    

    像Ton Plooij建议的那样,为控制器函数使用不同的方法名

    [HttpGet, ActionName("Index")]
    public ActionResult GetIndex()
    {
        ...
    }
    
    [HttpPost, ActionName("Index")]
    public ActionResult PostIndex()
    {
        ...
    }
    

    您是否尝试将数据从控制器传递到
    索引
    视图?操作名称不限于“索引”您是否尝试将数据从控制器传递到
    索引
    视图?操作名称不限于“索引”