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 :)
Post
from search submit button. – derloopkat