繁体   English   中英

ASP NET Core (MVC) 将参数从视图传递到控制器的问题

[英]ASP NET Core (MVC) problem with passing parameters from the view to the controller

我在视图中有两个 DropDownList。 当我尝试传递这些参数时,调用控制器中的方法但参数等于空值。

当我检查浏览器(F12-network)时,我会观察参数 - 它们已发送但在方法中仍然为空.

PS 我尝试更改 List 或 Location 和 JobTitle 或 CommonEntity 上的参数类型,但它不起作用

控制器:

 public class HelloController: Controller
{
 
    [HttpGet]
    public IActionResult Index()
    {
          
        var locations = new List<Location>()
        {               
               new Location()
               {
                    Id = 0,
                Title = "Russia"
               
            },                
                new Location()
               {
                    Id = 1,
                Title = "Canada"
               }
        };                   
        
        ViewBag.Location = locations;

        var jobs = new List<JobTitle>()
        {
            new JobsTitle()
            {
                Id = 0,
                Title = "Manager"
            } ,
            new JobsTitle()
            {
                Id = 1,
                Title = "Programmer"
            }
        };

        ViewBag.JobTitle = new SelectList(jobs, "Title", "Title");


        return View();
    }

    [HttpPost]
    public string Find(string answer1, string answer2)
    {
        return "Fine";
    }

看法:

@using Stargate.Core.Models.CoreEntities
@model CommonEntity

@using (Html.BeginForm())
{


@Html.DropDownListFor(m => m.Location.Title, new SelectList(ViewBag.Location, "Title", "Title"))
@Html.DropDownListFor(m => m.JobTitle.Title, new SelectList(ViewBag.JobTitle, "Title", "Title"))

<button type="submit">Find</button>

}

楷模:

public class CommonEntity
{
    public Location Location { get; set; }       
    public JobTitle JobTitle { get; set; }

}


public class JobTitle
{
    public long Id { get; set; }
    public string Title { get; set; }
}

 public class Location
 {
    public long Id { get; set; }
    public string Title { get; set; }
 }

你做错事,

  • 您应该更正您的 cshtml,以便在提交表单时,它将针对您的查找操作,
@using (Html.BeginForm("Find", "Hello"))
  • 在您的 Find Action 中,您应该提供可由 DefaultModelBinder 解析的输入参数,因为您没有 ViewModel 来拦截响应,我建议您接收一个FormCollection并且您可以从那里访问您的值。
[HttpPost]
    public string Find(FormCollection form)
    {
        return "Fine";
    }

尝试更新如下参数。 有关更多详细信息,请参阅ASP.NET Core 中的模型绑定

[HttpPost]
public string Find(Location Location, JobTitle JobTitle)
{
    return "Fine";
}

或者您可以尝试使用如下所示的CommonEntity参数。

[HttpPost]
public string Find(CommonEntity commonEntity)
{
    var locationTitle = commonEntity.Location.Title;
    var jobTitle = commonEntity.JobTitle.Title;
    
    return "Fine";
}

因为您接受的参数名称是answer1answer2 ,所以您的视图中应该有一个匹配的名称才能成功绑定。

你可以修改你的前端代码如下( DropDownListForDropDownList ):

@model CommonEntity
@using (Html.BeginForm("Find", "Hello"))
{
@Html.DropDownList("answer1", new SelectList(ViewBag.Location, "Title", "Title"))
@Html.DropDownList("answer2", new SelectList(ViewBag.JobTitle, "Title", "Title"))
<button type="submit">Find</button>
}

您的控制器:

public class HelloController : Controller
{
    [HttpGet]
    public IActionResult Index()
    {

        var locations = new List<Location>()
    {
           new Location()
           {
                Id = 0,
            Title = "Russia"

        },
            new Location()
           {
                Id = 1,
            Title = "Canada"
           }
    };

        ViewBag.Location = locations;

        var jobs = new List<JobTitle>()
    {
        new JobTitle()
        {
            Id = 0,
            Title = "Manager"
        } ,
        new JobTitle()
        {
            Id = 1,
            Title = "Programmer"
        }
    };

        ViewBag.JobTitle = jobs;


        return View();
    }

    [HttpPost]
    public string Find(string answer1,string answer2)
    {
        return "Fine";
    }
}

班级:

 public class CommonEntity
{
    public Location Location { get; set; }
    public JobTitle JobTitle { get; set; }

}
public class JobTitle
{
    public long Id { get; set; }
    public string Title { get; set; }
}
public class Location
{
    public long Id { get; set; }
    public string Title { get; set; }
}

结果: 在此处输入图片说明 在此处输入图片说明

暂无
暂无

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

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