Pass an argument from view to partial view action - asp.net-mvc

I have partial view like this defined in Products controller:
public PartialViewResult _Comments(int productId)
{
var comments = _CommentsRepo.GetCommentsByProductId(productId);
return PartialView(comments);
}
Partial view is in Shared folder:
In Products view I wrote something like:
#{Html.RenderPartial("_Comments", new { productId = Model.Id });}
but it seems I can't find best overload for me.
It seems I can also use #Html.Action helper.

RenderPartial renders a partial view directly - it doesn't call an action. You need to use RenderAction:
#{ Html.RenderAction("_Comments", new { productId = Model.Id }); }
Or just Action:
#Html.Action("_Comments", new { productId = Model.Id });

Related

Pass list of categories to partial view

I have a partial view which will display list of Main Categories and under each Main Category all of its subcategories. But the problem is I don't know how can I pass this Category List to my partial view. Please check the code bellow. I've also attached my .edmx table map picture to give you better idea. Once I pass it to partial view I want to loop though all categories and sub categories to display them
[ChildActionOnly]
public PartialViewResult _GuestNav()
{
using (var db = new TestEntities())
{
db.Categories.ToList(); // get list from here
return PartialView("_GuestNav"); // then pass that list to partial view
}
}
Here is the main action code:
public ActionResult Categories()
{
using (var dbCtx = new DbContext())
{
var categories = dbCtx.Categories.Include(x => x.SubCategories).ToList()
return View(categories);
}
}
Then in your Categories.cshtml you will have the code as below:
#model IEnumerable<Categories>
<ul>
#foreach(var category in Model)
{
<li>#category.CategoryName
#if(category.SubCategories.Any())
{
Html.RenderPartial("~/Partial/_SubCategory.cshtml", category.SubCategories);
}
</li>
}
</ul>
At last you supply a partial view called _SubCategory.cshtml in the Partial folder of Category folder as below:
#model IEnumerable<SubCategory>
<ul>
#foreach(var subCategory in Model)
{
<li>#subCategory.SubCategoryName</li>
}
</ul>
In your case if you want to pass this list to the partial view you specified you can do it as below:
[ChildActionOnly]
public PartialViewResult _GuestNav()
{
using (var db = new TestEntities())
{
var categories = db.Categories.Include(x => x.SubCategories).ToList(); // Added the include if you want to add subcategories as well
return PartialView("_GuestNav", categories); // then pass that list to partial view
}
}
Yo can use model binding, pass a Model or ViewModel as a parameter and access it from the partial view. For example, in your _GuestNav action:
...
return PartialView("_GuestNav",db.Categories.ToList());
Here's a link on how to accomplish that.
Then you can bind the model in your view. For example:
...
#model IEnumerable<Categories>;
For more detail, check out the examples from the link.
The PartialView method has an override that accepts an object. You need to store the results of the db.Categories.ToList() call in a variable and pass that to the method like this:
using (var db = new TestEntities())
{
var cats = db.Categories.Include("SubCategories").ToList(); // get list from here
return PartialView("_GuestNav", cats); // then pass that list to partial view
}
Just make sure your partial view expects a list of categories as its model. Then, inside your view you can iterate over the model and display the subcategories.
You should also look into how to use a viewmodel for your views.
EDIT
You may need to use an include statement since navigation properties are generally lazy loaded. Updated my answer.

Passing Data to View

I'm new to MVC and trying to pass data from a view to page and am having two problems:
The ID that is in the page url is not being passed to the controller
(customers/details/1)
I cannot get the variable to be written to the page. (i've been told
to try avoiding the use of viewbag and viewdata).
My controller looks like this:
public class CustomersController : Controller
{
public ActionResult Details(int? pageIndex)
{
var Name = "Nope";
if(pageIndex == 1)
{
Name = "John Smith";
};
return View(Name);
}
}
}
My view look like this:
#{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Details</h2>
<p>#Model.Name</p>

mvc passing data between controller and view

I am developing an application. I have created a view and a controller. The view has a button, on the click of which I am supposed to do database operations. I have put the database operations in the model, I am creating the object of model in the controller. On clicking the button the action is handled by a method in the controller, and the object of the model is created to get the records from the database. I would like to know if there is any way to display this data in the view.Is the approach correct or the view is supposed to interact with model directly to get the data.
Following is the code in controller that gets invoked on the button click
public ActionResult getRecord()
{
DataModel f_DM = new DataModel();
DataTable f_DT = f_DM.getRecord();
return View();
}
DataModel is the model class with simply a method "getRecord".
Any help will be highly appreciated.
I would like to add that i am using vs2010 and mvc4
Regards
you should write the logic of retrieving data in your controller. Store all your data in view model and pass it to the view.
for eg.
Model
namespace Mvc4App.Models
{
public class Product
{
public string Name { get; set; }
}
public class ProductViewModel
{
public Product Product { get; set; }
public string SalesPerson { get; set; }
}
}
Controller
public class ProductController : Controller
{
public ActionResult Info()
{
ProductViewModel ProductViewModel = new ProductViewModel
{
Product = new Product { Name = "Toy" },
SalesPerson = "Homer Simpson"
};
return View(ProductViewModel);
}
}
View
#model Mvc4App.Models.ProductViewModel
#{ ViewBag.Title = "Info"; }
<h2>Product: #Model.Product.Name</h2>
<p>Sold by: #Model.SalesPerson</p>
This is the best known practice to pass data from controller to the view.
you may use other techniques also like,
1. ViewData
2. ViewBag
3. TempData
4. View Model Object
5. Strongly-typed View Model Object
Yes, it's possible, but actually now very logical way to to this.
Lets follow your way. You have some View were you have a button, that will trigger this action.
For ex:
public ActionResult Index()
{
return View();
}
Inside view you can have a Ajax link, that will trigget your getRecord method:
<div id="GetDataDiv"></div>
<div>
#Ajax.ActionLink("Get Record", "getRecord", "ControllerName", null, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "GetDataDiv" })
</div>
In the getRecord method you should have:
public ActionResult getRecord()
{
DataModel f_DM = new DataModel();
DataTable f_DT = f_DM.getRecord();
return PartialView(f_DT);
}
And in View it should be:
#model DataTable
#Model.PropertyOne #Model.PropertyTwo
It should works for you.
Actually same exaple here: http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=151

foreach loop in mvc razor to iterate each element from the model and display

I want to Display each element on Razor view from Model through foreach loop,I have no code in Controller
when I run the Application , I get the Error:
Object reference not set to Instance of an Object
Please some body help me, I wrote the code in the View
#model IEnumerable<Models.Web.Category>
#foreach(var item in Model){
#item.CategoryName
}
and My Controller is
public ActionResult Category(){
return View();
}
I mean something like this:
public ActionResult Category(){
var categories = db.Categories;
return View(categories);
}
OR
public ActionResult Category(){
List<Category> categories = new List<Category>();
categories.Add(new Category() { ID = 1, Name = "Bikes" });
categories.Add(new Category() { ID = 2, Name = "Cars" });
categories.Add(new Category() { ID = 3, Name = "Trucks" });
return View(categories);
}
You should initialize your model in controller...
Model is null.
If you want to display data from the model, you need to pass a model from the controller.
Look your view is expecing a Model:
#model IEnumerable<Models.Web.Category>
In your Controller you're not passing anything to the view, so the view has null as a Model.
You need to create your collection IEnumerable<Category> and pass it to the view.

Execute Controller Method from razor html view in MVC?

Ok so I have an Html.DropDownList and I want to be able to execute a controller method ActionResult output(string test) and send a parameter to it. I have something like this already but I get an Uncaught TypeError: Cannot set property 'action' of null message:
#Html.DropDownList(
"revisions", ViewData["revisions"] as SelectList,
new
{
onchange = "this.form.action = '/Shops/output('test')'; this.form.submit();"
})
How do I go about fixing my code?
If your Action method's parameter name is id,
public ActionResult output(string id)
{
//do something
}
then you may use your form action url like this.(The default routing will take care of rest)
/Shops/output/somestringhere.
If you have a different name, use that as the query string
public ActionResult output(string name)
{
//do something
}
Now use your form action url like
/Shops/output?name=somestringhere
Another suggestion about your code is to avoid Viewdata for rendering the dropdown. Try to use strongly typed view model and it's properties for transfering data to your view. Also try to move your javascript from your view and make it unobutrusive. So that your view stays as clean markup only.
Assuming you want to show a Revision dropdown in a document create view, Add a property to your viewmodel to have the dropdown items.
public class DocumentCreateViewModel
{
//Other properties also here
public List<SelectListItem> Revisions{ set;get;}
public int SelectedRevision { set;get;}
public DocumentCreateViewModel()
{
Revisions=new List<SelectListItem>();
}
}
and in your GET action, fill the dropdown content to the Revisions property.
public ActionResult Create()
{
var vm=new DocumentCreateViewModel();
vm.Revisions=GetRevisionItemsFromSomeWhere();
return View(vm);
}
And in your strongly typed view,
#model DocumentCreateViewModel
#using(Html.Beginform())
{
#Html.DropDownListFor(x => x.SelectedRevision,
new SelectList(Model.Revisions,"Value","Text"), "Select..")
<input type="submit" />
}
Now to handle the form submit on change event of dropdown, add this script.
$(function(){
$("#SelectedRevision").change(function(){
var _this=$(this);
var selectedRevision=_this.val();
$("form#YourFormIDHere")
.attr("action","./Shops/output/"+selectedRevision).submit();
});
});
Instead of hardcoding the url to shops/output, you may use the razor helper method(#Url.Action) to get the proper path.
#Html.DropDownList(
"revisions", ViewData["revisions"] as SelectList,
new
{
onchange = "submitForm();"
})
and your Javacript goes here
function submitForm()
{
var form = document.forms[0];
form = '/Shops/output?test=test';
form.submit();
}

Resources