mvc视图返回的是html,如何在不将数据传递到Web网格的情况下返回默认视图(空白HTML)?MVC C#...

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. :\

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值