0
votes

I have my home index which use to display various parts of the application. it contains many partial views. I'm having no problem for displaying data on partial view, but when I try to make one of my my partial view doing something like searching the item. I have search partial view where it take input like Street name, Country to display list of the property.

My index uses viewModel like

public class HomeViewModel
{
    public List<Ads> Ads { get; set; }
    public AdsImage AdsImages { get; set; }
    public List<Category> Category { get; set; }
    public List<City> Cities { get; set; }
    public List<County> Counties { get; set; }


    //ViewModel for _AdSeach paritrial view.
    public Search Search { get; set; }
}

My search partiral will use HomeViewModel to display text fields butin this partial view it will use other controller for firing the data. it wont use home controller but it will use Ads controller with SearchAds action. My partial view will look like this.

<div class="row">
    <div class="col-sm-3">
        @Html.TextBoxFor(m => m.Search.Category, new { placeholder = "Category", @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Search.Category)
    </div>
    <div class="col-sm-3">
        @Html.TextBoxFor(m => m.Search.City, new { placeholder = "City", @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Search.City)
    </div>
    <div class="col-sm-3">
        @Html.TextBoxFor(m => m.Search.County, new { placeholder = "County", @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Search.County)
    </div>
    <div class="col-sm-2">
        <button type="submit" class="btn btn-primary">ค้นหา</button>
    </div>
</div>

In my home index will use Kaiban.ViewModel.HomeViewModel with partial view render like this

@Html.Partial("_AdsSearch", Model.Search)

In my Seach Model which use to handle data which will be sent from partial view and business logic looks like this.

[NotMapped]
public class Search
{
    public int? Id { get; set; }
    public string Category { get; set; }
    public string City { get; set; }
    public string County { get; set; }



}
[NotMapped]
public class ProductBusinessLogic
{
    private ApplicationDbContext Context;
    public ProductBusinessLogic()
    {
        Context = new ApplicationDbContext();
    }


    //คลาสที่ใช้ในการค้นหารายชื่อโฆษณา
    public IQueryable<Ads> GetProducts(Search searchModel)
    {
        var result = Context.Adses.AsQueryable();
        if (searchModel != null)
        {
            if (searchModel.Id.HasValue)
                result = result.Where(x => x.Id == searchModel.Id);

            //เช็คว่า SreetName มีค่ส่งมาหรือปล่าวเเล้ว ค้นหาจากฐานข้อมูลโดยกำหนด StreetName ของ Search Model ตรงกับชื่อถนนไหนบ้างในฐานข้อมูล;
            if (!string.IsNullOrEmpty(searchModel.Category))
                result = result.Where(x => x.Category.Name.Contains(searchModel.Category));

            if (!string.IsNullOrEmpty(searchModel.County))
                result = result.Where(x => x.County.Name.Contains(searchModel.County));

            if (!string.IsNullOrEmpty(searchModel.City))
                result = result.Where(x => x.City.Name.Contains(searchModel.City));
        }
        return result;
    }
}

And this is my ads controller

public ActionResult SearchAds(Search searchModel)
    {
        var business = new ProductBusinessLogic();
        var model = business.GetProducts(searchModel);
        return View(model);
    }

When I submit the button on partial view, somehow my model receive null value. Even if I type to all field in partial view.

I still have no idea why it receive null. can someone please help me to fix this problem. and also if someone could give me better understanding of how view and model works for sending and receiving data.

Thank you in advance for any help :)

2
In your question please include the controller method that receives the Post from search submit button.derloopkat
Hi, I have added my ads controller if you please can have a look.Tsubagi Yoshino

2 Answers

0
votes

What's your model declaration for your partial?

From @Html.Partial("_AdsSearch", Model.Search), it seems like you're passing Search model instead of HomeViewModel to the partial, but then from how you bind the model properties to inputs, i.e., @Html.TextBoxFor(m => m.Search.Category), it seems like you're passing the HomeViewModel instead.

Try changing the model declaration on your partial to:

@model Search

<div class="row">
    ...
0
votes

I just found the solution by using this partial helper

with my previous render I tried to use the same viewModel which is HomeViewModel and passing everything to this model. I thought it was only way to display my partial in home index.

With this code allows me to use partial from different Model.

@Html.Partial("_AdsSearch", new Kaiban.Models.Search())

In my partial view, I changed model to application.model.Search instead of application.viewModel.HomeViewModel and also the same with

@Html.TextBoxFor(m => m.Search.City)

to

@Html.TextBoxFor(m => m.City)