I have my default ActionResult Index() class which used to be:
return View();
I've recently added a Web Grid that is populated when the user enters search fields and clicks a 'Search' button. However, my Index() now throws an error because my View is expecting an IEnumerable object to be passed to the view from the Controller. I've tried passing an object with empty data to avoid the page being returned a view without data, but it seems to me that the page is always running this ActionResult Index() function so the page loads with the blank object after the search is performed.
How can I differ between the two scenarios? I want to return my object when a user performs a search and I want to want to load a blank grid when the page is initially loaded. I do not know of the life cycle of an MVC application as I have just started learning MVC this week.
Here is my View:
@model IEnumerable
@{
ViewBag.Title = "Amazing";
}
@Scripts.Render(myNameSpace.Web.App.BundleConfig.GetVirtualPathForScript(myNameSpace.Web.App.BundleConfig.Scripts.PatientMerge))
@using System.Web.Helpers;
/*CSS overrides*/
.main {
width: 100%;
}
.container {
width: 100%;
}
.main-container {
background: none;
}
.header {
width: 100%;
}
.col-left {
display: none;
}
/*Patient Merge CSS*/
.searchOneWrp {
float: left;
display: inline-block;
width: 49%;
margin-right: 0.5%;
}
.searchTwoWrp {
float: right;
display: inline-block;
width: 49%;
margin-left: 0.5%;
}
Patient Merge
Patient One Search
First Name:
Last Name:
D.O.B.:
title="Search">
Search
@{
myNameSpace.Repository.Model.PatientMerge patient = new myNameSpace.Repository.Model.PatientMerge();
}
@{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 15, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "grdPatientOneSearch");
//grid.Bind(rowCount: 1, source: Model, autoSortAndPage: false);
grid.Pager(WebGridPagerModes.NextPrevious);
}
@grid.GetHtml(tableStyle: "webgrid",
headerStyle: "header",
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns(
grid.Column("pat_id", "Id"),
grid.Column("fname", "First Name"),
grid.Column("lname", "Last Name"),
grid.Column("birth_date", "dob")
))
Patient Two Search
First Name:
Last Name:
D.O.B.:
title="Search">
Search
Here is my Controller (that is currently throwing an error because I am passing a view without the expected IEnumerable):
namespace myNameSpace.Web.App.Controllers.Patient
{
public class PatientMergeController : Controller
{
//
// GET: /PatientMerge/
public ActionResult Index()
{
return View();
}
//
// GET: /PatientMerge/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /PatientMerge/Create
public ActionResult Create()
{
return View();
}
//
// POST: /PatientMerge/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /PatientMerge/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /PatientMerge/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /PatientMerge/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /PatientMerge/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
#region Search Function
public ActionResult GetPatientOneSearch(string fname, string lname, string dob)
{
try
{
using (var target = new PatientRepository())
{
var result = target.GetPatient(fname, lname, dob, "", "", "");
List patientList = new List();
for (int i = 0; i < result.Count; i++)
{
myNameSpace.Repository.Model.PatientMerge patient = new myNameSpace.Repository.Model.PatientMerge();
patient.pat_id = result[i].PatientId;
patient.fname = result[i].FirstName;
patient.lname = result[i].LastName;
patient.birth_date = result[i].DateOfBirth.ToString();
patientList.Add(patient);
}
return View(patientList);
//return Json(new { success = result }, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
Logger.Log.Error(ex.Message + ex.StackTrace + ex.InnerException);
//return Json(new { error = ex.Message }, JsonRequestBehavior.AllowGet);
return View();
}
}
#endregion
}
}
And the model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace myNameSpace.Repository.Model
{
public class PatientMerge
{
public int pat_id { get; set; }
public string pag_status_cn { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public string birth_date { get; set; }
public string AgeYears { get; set; }
public string gender_cd { get; set; }
public string phone_no { get; set; }
public string addr1 { get; set; }
public string addr2 { get; set; }
public string city { get; set; }
public string state_cd { get; set; }
}
}
Talk1:
Returning a new List without nothing?
Solutions1
Just return an empty list when calls Index directly:
public ActionResult Index()
{
return View(new List());
}
OR
Or treat in the view If the List comes null to avoid the issue.
@if(Model == null)
{
//block that need the list
}
Talk1:
I have attempted passing a blank list as you suggested. That did not work as the page always loads the blank list, regardless of a search being performed. I believe handling a null Model is the correct answer, but I am not entirely sure how to go about doing that. I tried wrapping the grid in the @if(Model == null) statement, but I guess it didn't like that?
Talk2:
just surround the area of the view that uses the Model to avoid this issue.
Talk3:
if(Model != null) { //create grid } always evaluates to true. :\