mvc3 partial view error - asp.net

I want to make a form for entering customer data. It consists of several text boxes and a combobox. And it is the whole problem lies in this combobox. When I trying to render this partialview , gets error: "Object reference not set to an instance of an object."
This is partialview controller code
public PartialViewResult GetStates()
{
var states = from s in conn.order_data select s.state;
return PartialView(states.ToList());
}
GetStates partialview
#model IEnumerable<bookstore.state>
#foreach (var item in Model) {
<select>
<option>#item.STATE_Name</option>
</select>
}
part of main view
<div class="editor-field">
#{Html.RenderPartial("GetStates");}
</div>
Please, help

Unless you are loading the view dynamically (in which case you could do it with jquery get)
here is how you could do it
Controller:
public ActionMethod MainView()
{
var model = new myMainModel { States = from s in conn.order_data select s.state };
return View()
}
Main View:
#Html.Partial("MyPartialViewName", Model.States);

Try this:
if(states != null)
{
return PartialView(states.ToList());
}
return PartialView();

Related

ERROR: Unable to cast object of type 'System.Collections.Generic.List' to type 'X.PagedList.IPagedList'

I use List instead of IEnumerable model
My Controller
public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
var itemCount = employees.ToPagedList(pageNumber, 5);
return View(employees.ToList());
}
My View
#Html.Partial("EmployeeList", Model.AsEnumerable())
#Html.PagedListPager((IPagedList)Model.AsEnumerable(), page => Url.Action("Index", new { page }))
IEnumerable<EmployeeViewModel> can't be directly cast to IPagedList with (IPagedList)Model.AsEnumerable() since they're different instances. You should return a PagedList instance using ToPagedList method as View argument (assumed employees is an List<EmployeeViewModel> or array of viewmodels):
public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
return View(employees.ToPagedList(pageNumber, 5));
}
And use the bound model inside PagedListPager like this:
#model PagedList.IPagedList<EmployeeViewModel>
#using PagedList.Mvc;
#Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
And pass the IPagedList through Model in HtmlHelper.Partial:
#Html.Partial("EmployeeList", Model)
Similar issue:
How to load first x items and give user the option to load more in MVC.NET
**Controller Action **
public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
return View(employees.ToList().ToPagedList(pageNumber, 5));
}
View page
#using PagedList
#using PagedList.Mvc
#model IEnumerable <Employee>
#Html.Partial("EmployeeList", Model)
#Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page }))

Paging on Partial View

I have a main view with a partial view.
The partial view allows paging, I am using Bootstrap.
Controller:
public ActionResult Main()
{
return View();
}
//[ChildActionOnly]
public ActionResult _Partial(int page)
{
Model = //returns records for requested page
return View(Model);
}
View:
<ul class="pagination">
#for (int i = 1; i <= model.TotalPages; i++)
{
<li>#Html.ActionLink(i.ToString(), "_Partial", new { page = i, })</li> }
}
</ul>
My problem is, although the paging works, selecting a page is calling the _Partial Action directly ( after I removed [ChildActionOnly]
So on paging it loads the Partial View only without the Main View which it forms part of...therefor it is missing the 'frame' provided by the Main View or any formatting.
Should I be following a different approach or is there a way to call a Partial Action and it’s Main Action?
I guess I would have liked to do something like…
<li>#Html.ActionLink(i.ToString(), "Main + _Partial", new { page = i, })</li>
Or how can I get Bootstrap Paging (I don't want to go the Ajax route) to work on a Partial View?
If you don't want to use ajax, then you have to return the whole page. Here an example for your case:
Controller:
public ActionResult Main()
{
return View();
}
public ActionResult SomeAction(int page) //Name this as your like
{
// I use viewbag here for convenience,
//you should consider to create new view model if necessary.
ViewBag.PageNum = page;
return View();
}
//[ChildActionOnly]
public ActionResult _Partial(int page)
{
Model = //returns records for requested page
return View(Model);
}
View "SomeAction.cshtml":
#*Put your Main page content here*#
#Html.Action("SomeAction", "YourControllerName", new { page = ViewBag.PageNum })
#*Put your Main page content here*#
And Your pagination should change to:
<ul class="pagination">
#for (int i = 1; i <= model.TotalPages; i++)
{
<li>#Html.ActionLink(i.ToString(), "SomeAction", new { page = i, })</li> }
}
</ul>
Keep in mind that I created the "SomeAction" only because I don't know what exactly does your "Main" action do. If the "Main" is simply the 1st page, then you should merge the "SomeAction" and "Main" into one:
public ActionResult Main(int page = 1)
{
ViewBag.PageNum = page;
return View();
}

Getting error in mvc4

Error:-
Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
My Main page View is this
{
ViewBag.Title = "Start";
}
<h2>Start</h2>
<script type="text/javascript">
$(document).ready(function () {
$("#test1").click(function (e) {
$("#firstpartialview").css("display", "none");
$("#secondpatialview").css("display", "none");
$("#firstpartialview").css("display", "block");
});
$("#test2").click(function (e) {
$("#firstpartialview").css("display", "none");
$("#secondpatialview").css("display", "none");
$("#secondpatialview").css("display", "block");
});
});
</script>
<a id="test1"></a>
<a id="test2"></a>
<div id="firstpartialview">#Html.Action("FirstView", "Home") </div>
<div id="secondpatialview">#Html.Action("SecondView", "Home") </div>
My controller is this:-
public ActionResult Start()
{
return View();
}
public ActionResult FirstView()
{
ModelA objA = new ModelA();
return PartialView(objA);
}
public ActionResult SecondView()
{
ModelB objB = new ModelB();
return PartialView(objB);
}
My Partial View is this
_partialA.cshtml
#model demo3.Models.ModelA
#{
ViewBag.Title = "_partialA";
}
<h2>_partialA</h2>
<div>#Html.EditorFor(m => m.EmployeeId) </div>
<div>#Html.EditorFor(m => m.EmployeeName)
and another partial view is this
_partialB.cs.html
#model demo3.Models.ModelB
#{
ViewBag.Title = "_partialB";
}
<h2>_partialB</h2>
<div>#Html.EditorFor(m => m.Comapny) </div>
<div>#Html.EditorFor(m => m.FisacalYear) </div>
Please help me to solve the error..on browser this error is coming
The partial view 'FirstView' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/FirstView.aspx
~/Views/Home/FirstView.ascx
~/Views/Shared/FirstView.aspx
~/Views/Shared/FirstView.ascx
~/Views/Home/FirstView.cshtml
~/Views/Home/FirstView.vbhtml
~/Views/Shared/FirstView.cshtml
~/Views/Shared/FirstView.vbhtml
The error means that mvc is trying to load a file (by default, if you haven't specified the view file name, it checks by Action name which in this case, is FirstView), but it can't find it.
You can specify the view file name in your return statement:
return PartialView(string "View", object model)
This can be used like so (assuming _partialA is your view's cshtml filename):
return PartialView("_partialA", objA);
The error is quite specific. This line is causing your error:
<div id="firstpartialview">#Html.Action("FirstView", "Home") </div>
It can't find the view FirstView. You need to place the view in one of the locations that the error message tells you or change where the view engine searches for your views.

How to Display multiple images from db using MVC3

I am new to MVC.I have saved the image and some data but can't display the saved images. I want to display all saved images in the main page.
Model: Get the db list from model
public List<Products> GenreList()
{
}
Controller
public ActionResult MYsample()
{
var MyList = storeDB.GenreList();
var a= MyList.Count;
if (a != null)
{
foreach (var li in MyList)
{
return File(li.fileContent, li.mimeType,li.fileName);
}
}
return View(MyList);
}
View
#foreach (var item in Model)
{
<img src="#Url.Action("MYsample", "HomeController", new { id = item.ProductID })" alt="#item.Productname" />
}
You could start by writing a controller action that will serve the images to the response stream:
public ActionResult Image(int id)
{
// you should add a method to your repository that returns the image
// record from the id
Products image = storeDB.Get(id);
return File(image.fileContent, image.mimeType);
}
and then in your main controller action send the list of images to the view:
public ActionResult MySample()
{
var model = storeDB.GenreList();
return View(model);
}
and then in your strongly typed view loop through the images and generate an <img> tag for each image by pointing its src property to the newly created controller action:
#model MyList
#foreach (var li in MyList)
{
<img src="#Url.Action("Image", new { id = li.Id })" alt="" />
}
If you don't want to have a separate controller action that will query the database and retrieve the image record from an id you could use the data URI scheme. Bear in mind though that this is not supported by all browsers.
So the idea is that your controller action will send the image data to the view:
public ActionResult MySample()
{
var model = storeDB.GenreList();
return View(model);
}
and then inside your strongly typed view you could loop through the list and generate the proper <img> tag:
#model MyList
#foreach (var li in MyList)
{
<img src="src="data:#(li.mimeType);base64,#(Html.Raw(Convert.ToBase64String(li.fileContent)))" alt="" />
}

Making Partial View Reusable

Say I have a partial view that renders a dropdown list of Applications. When selecting an item in the dropdown it renders another partial view.
This dropdown list exists in a few places in the application but on each page a different partial view needs to be rendered when selecting an application. Is there an easy way to make the dropdown reusable? ie I need to set a different data_url depending on which page the partial view is rendered.
Partial View:
<script type="text/javascript">
$(function () {
$('#ApplicationsDropdownList').change(function () {
var url = $(this).data('url');
var applicationId = $(this).val();
$('#RolesForApplication').load(url, { applicationId: applicationId})
});
});
</script>
<div>
<label for='ApplicationsDropdownList'>Application:</label>
#Html.DropDownListFor(
x => x.SelectedApplicationId,
new SelectList(Model.Applications, "Value", "Text"),
"-- Select Application --",
new
{
id = "ApplicationsDropdownList",
data_url = Url.Action("ViewRolesTableForApplication", "Index")
}
)
</div>
Controller:
public ActionResult ViewRolesTableForApplication(string applicationId)
{
...
return View("_RolesTableForApplicationPartial");
}
You could add a string property containing the data_url to the model that you use for your partial view.
So in addition to the Model containing Applications it will have public string DataUrl { get; set; } as well.

Resources