简体   繁体   English

在 ASP.net MVC 中搜索多个字段:将参数从视图传递到 controller

[英]Searching Multiple Fields in ASP.net MVC: Passing parameters from view to controller

This is in relation to post Filter/Search using Multiple Fields - ASP.NET MVC .这与使用多个字段的后过滤器/搜索有关 - ASP.NET MVC I am new to ASP and MVC logic, so I am not quite sure how to actually assign the values that are typed in the input box to values in the model (basically pass the search parameters from view to controller, where I am referencing ProductSearchModel as an argument).我是 ASP 和 MVC 逻辑的新手,所以我不太确定如何将输入框中输入的值实际分配给 model 中的值(基本上将搜索参数从视图传递到 controller,我在这里引用 ProductSearchModel 作为一个论点)。 I am familiar with ViewData, however, in my index page, I am using the Product model, not the ProductSearchModel.我熟悉 ViewData,但是,在我的索引页面中,我使用的是Product model,而不是 ProductSearchModel。 I could not comment under the thread but I really hope someone can help with this.我无法在该主题下发表评论,但我真的希望有人可以提供帮助。 Thank you so much.太感谢了。

EDIT: So just as described in the referenced post above, I have a DonationSearchModel class, a DonationSearchLogic class, and a DonationView class.编辑:正如上面引用的帖子中所述,我有一个DonationSearchModel class、一个DonationSearchLogic class 和一个DonationView class。

In my DonationController:在我的捐赠控制器中:

 public ActionResult Index(DonationSearchModel searchModel)
         {
            
             var donations = from m in _context.Donation
                            .Include(p => p.Person)
                            .AsNoTracking()
                             select m;

            
            var dv = new DonationView();

            var search = new DonationSearchLogic(_context);            
            var model = search.GetDonations(searchModel);
            dv.Donations = model;

            return View(dv);
            //return View(await donations.ToListAsync());
         }

In my Index.cshtml:在我的 Index.cshtml 中:

@model TissueBankApp.Models.View.DonationView
<form Donation asp-action="Index" method="get">
    <p>
        @using (Html.BeginForm("Index", "DonationController", FormMethod.Get))
        {
            <label class="control-label">Forename</label>
            @Html.TextBoxFor(m => m.DonationSearchModel.Forename)

            <label class="control-label">Surname</label>
            @Html.TextBoxFor(m => m.DonationSearchModel.Surname)

            <label class="control-label">DOB</label>
            @Html.TextBoxFor(m => m.DonationSearchModel.DOB)

            <input type="submit" class="btn btn-outline-primary btn-sm" value="Search" />
        }
    </p>
</form>

My DonationView:我的捐款视图:

namespace TissueBankApp.Models.View
{
    public class DonationView
        {
            //
            // TODO: This was thrown together to get the associated view working. It needs to be replaced with a proper viewmodel containing all required attributes, and related methods to map those attributes to the data models.
            //
    
            public Donation Donation { get; set; }
            public Person Person { get; set; }
            public Contact PlaceOfDeathContact { get; set; } 
            public Contact ConsentContact { get; set; }
            
            public Contact FuneralDirectorContact { get; set; }
            public DonationSearchModel DonationSearchModel { get; set; }
            public string CaseType { get; set; }
    
            public IEnumerable<Donation> Donations { get; set; }
    
    
        }

}

At the moment, the display is fine, but when I actually search (for example search "Anna" in the forename field) and debug the Index function, the forename property is null, so it just displays everything since there are no searched-for strings.目前显示很好,但是当我实际搜索(例如在名字字段中搜索“Anna”)并调试索引function时,名字属性是null,所以它只显示所有内容,因为没有搜索到字符串。 It seems like the search string is not being passed from view to controller and I cannot figure out why.似乎搜索字符串没有从视图传递到 controller,我不知道为什么。 I hope this gives you more context!我希望这能给你更多的背景!

According to your comment, i want to share solution for your problem.根据您的评论,我想分享您的问题的解决方案。 Of course you can change get or post method depends on you, but you are right about being get method.当然你可以改变 get 或 post 方法取决于你,但你是正确的 get 方法。 Filtering methods are generally http get.过滤方式一般是http搞定。 There are two approach for get filtered value from view to controller: One way;有两种方法可以从视图中获取过滤值到 controller: 一种方法;

    [HttpGet]
    public ActionResult Index(DonationView viewModel)
     {
        DonationSearchModel search= viewModel.DonationSearchModel;
        //You can reach your search model over donationview
       
        return View();
        //return View(await donations.ToListAsync());
     }

Second way;第二种方式;

    [HttpGet]
    public ActionResult Index([Bind(Prefix = "DonationSearchModel")]DonationSearchModel donationSearchModel)
     {
        DonationSearchModel search= donationSearchModel;
        //You can reach your search model over donationSearchModel
       
        return View();
        //return View(await donations.ToListAsync());
     }

You can create a search filter class in order to use in view part.您可以创建搜索过滤器 class 以便在视图部分中使用。 For example;例如;

public class Product 
{
    public int Id { get; set; }

    public double Price { get; set; }

    public string Name{ get; set; }
 }

public class ProductSearchModel 
{
    public double MinPrice{ get; set; }

    public double MaxPrice{ get; set; }

    public string SearchName{ get; set; }
 }


[HttpPost]
public ActionResult Index(ProductSearchModel model)
{
    if (ModelState.IsValid)
    {
        //TODO: Your service method with viewmodel, then send value to view             
    }

    return View();
}

And you can use them in view part with html helpers on mvc to send or get value between controller-view.您可以在视图部分使用它们与 mvc 上的 html 助手一起在控制器视图之间发送或获取值。

@model Project.ProductSearchModel 
@using (Html.BeginForm("Index", "Home", FormMethod.Post))  
{  
<br />  

   @Html.LabelFor(m => m.MinPrice) 
   @Html.TextBoxFor(m => m.MinPrice)   
<br />  

   @Html.LabelFor(m => m.MaxPrice) 
   @Html.TextBoxFor(m => m.MaxPrice)    
<br />  
   @Html.LabelFor(m => m.SearchName) 
   @Html.TextBoxFor(m => m.SearchName) 
<br />  
<input type="submit" class="btn btn-outline-primary" value="Send" />

} 

Above codes are very basic usage.以上代码是非常基本的用法。 If you can explain more detail what you do in your project, I can help you more.如果您可以更详细地解释您在项目中所做的事情,我可以为您提供更多帮助。 Also you have net core version in your project, you can use tag helpers effectively.此外,您的项目中有网络核心版本,您可以有效地使用标签助手。

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

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