微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从视图向控制器传递参数的ASP NET CoreMVC问题

如何解决从视图向控制器传递参数的ASP NET CoreMVC问题

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

当我在浏览器(F12网络)中签入时,我会观察参数-已发送参数,但方法中的参数仍然为空

.

P.S。 我尝试更改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"))
@Html.DropDownListFor(m => m.JobTitle.Title,new SelectList(ViewBag.JobTitle,"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"))
  • 在“查找操作”中,您应该提供DefaultModelBinder可解析的输入args,因为您没有ViewModel来拦截响应,因此建议您接收FormCollection,并可以从以下位置访问值:在那里。
[HttpPost]
    public string Find(FormCollection form)
    {
        return "Fine";
    }
,

尝试如下更新参数。有关更多详细信息,请参阅Model Binding in 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"))
<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; }
}

结果: enter image description here enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。