0

i want to render partial view to the parent view which is tightly couple with another model

1- my _EmployeeSearch Partial view

@model ...Models.EmployeeSearchModel
@using (Html.BeginForm("Details", "Employee", FormMethod.Get, new { @class = "form-group" }))
{
 <text>First Name:</text> <span class="input-icon">
 @Html.TextBoxFor(model => model.FName)<i class="ace-icon fa fa-search nav-search-icon"></i>
 </span>
 <input type="submit" value="Search" class="btn btn-xs btn-yellow active" />
}

2- Parent View

@model IEnumerable<...Models.EmpPersonalInfoModel>
<div>
@Html.Partial("_EmployeeSearch", Model.FirstOrDefault().EmployeeSearchModel)
</div>

3- EmpPersonalInfoModel.cs

public class EmpPersonalInfoModel
{
 ...
 public string FName { get; set; }
 public EmployeeSearchModel { get; set; };
}  

4- EmployeeSearch.cs model

public class EmployeeSearchModel
{  
 public string FName { get; set; }  
}

5- and mycontroller method is

public ActionResult Details(EmployeeSearchModel searchModel)
{
 Service_Employee service_ = new Service_Employee();
 List<EmpPersonalInfoModel> emp_model = new List<EmpPersonalInfoModel>();
 if(searchModel == null)
   {  emp_model = service_.GetAllEmployees();  }
 if(searchModel !=null)
   {  emp_model = service_.SearchEmployee(searchModel);  }
 return View(emp_model);
}

But i get the error while rendering partial view into Parent view on this line expecting to have EmpPersonalInfoModel instead of EmployeeSearchModel

 @Html.Partial("_EmployeeSearch", Model.FirstOrDefault().EmployeeSearchModel)

Any one have idea about this error

lilli
  • 55
  • 6
  • Because `Model.FirstOrDefault().EmployeeSearchModel` is `null` –  Mar 13 '17 at 03:55
  • @StephenMuecke yes i know its null, but don't know how to handle it in controller code – lilli Mar 13 '17 at 04:00
  • You need to check if its `null`, and if so, initialize it as a new `EmployeeSearchModel` –  Mar 13 '17 at 04:02
  • @StephenMuecke when i try to initialize EmployeeSearchModel as new it does not comes in intellisense – lilli Mar 13 '17 at 04:18
  • You have not shown the code your trying - but `if (emp_model.First().EmployeeSearchModel == null) { model.First().EmployeeSearchModel = new EmployeeSearchModel() }; (and I assume your property is `public EmployeeSearchModel EmployeeSearchModel { get; set; };` - what you have shown does not compile) –  Mar 13 '17 at 04:23
  • @StephenMuecke sir actually i try to separate search fields from parent view to partial view and render it to main parent view. By default list of all employees are showing in parent view and when a user search employee by 'First Name' the list of employee will update accordingly. – lilli Mar 13 '17 at 04:39
  • Then your model does not make any sense (why would `EmpPersonalInfoModel` contain a property `EmployeeSearchModel`. Just remove that property and use `@Html.Partial("_EmployeeSearch", new EmployeeSearchModel())` –  Mar 13 '17 at 05:16
  • @StephenMuecke yes i already got it but thanks for you help i just replace EmployeeSearchModel to new appMVC.Models.EmployeeSearchModel – lilli Mar 13 '17 at 06:17

0 Answers0