Form elements and logic within a partial view - c#

I have a number of forms on my site, all of which are vastly different. There will however be an element which is shared by all - a few text boxes and a validation message depending on the input.
I'm using asp.net MVC3 and in the interests of code-reuse, I'd like to put this shared element into a partial view which will be included in every form. This partial view will have to perform the logic to check the input and display a validation message accordingly.
Does anyone have any pointers on how I can do this? Thanks in advance.

Create a ViewModel for your common view Say
public class CommonViewModel
{
public bool IsValid {get;set;}
public string Message {get;set;}
}
Now Say for some other ViewModel
public class MyOtherViewModel
{
public CommonViewModel CommonViewModel {get;set;}
//Other Properties here
}
Now from MyOtherView.cshtml you can render the partial view easily, passing the values from main view
#model MyOtherViewModel
#Html.Partial("commonView",Model.CommonViewModel)
I hope this should help you to start with.
cheers

I am writing a rough code, please correct accordingly.
Use a Parital View and write code as below.
#Html.BeginForm('actionname',controller)
{
#Html.TextBox('abc')
}
then create a method in your controller like
[Post]
Public ActionResult ActionName()
{
}
put this partialview in any of you page.

Related

ASP.NET MVC 5 passing data from home controller to layout via ViewBag

In my HomeController.cs I have some data, which I need to pass to my /Shared/_Layout.cshtml via ViewBag. But I have no idea, how can I do it.
This is my /Shared/_Layout.cshtml
#foreach (var item in ViewBag.Order)
{
<li>#item.Name [ #item.Count ]</li>
}
And here is HomeController.cs
public ActionResult Index()
{
ViewBag.Order = SELECT FROM DB -> ADD TO LIST
}
You don't have access to the Viewbag in the Layout View unfortunately.
You could however use:
PartialView call to a method in a base controller
Simply define a base controller that is a parent for all your controllers. This can also be handy for some error handling by the way.
And in your layout use #Html.Partial("ViewName") to call your base controller.
Use Ajax call
Use javascript in your layout view to execute a controller function that returns the data you need.
Use Session variables instead
since session variables are accesable in layout views.
There are probably more answers too, but I believe these will probably be the most common solutions to your problem.
(If you need any help implementing one of these solutions please give me a sign and I'll explain it more deeply how to do it.)
A fairly common method of passing content to the layout, is to let all ViewModel classes implement a base class (or interface), which will then be the #model of your _Layout view.
Assuming you want to pass on some text MyText to all of your pages:
// My base view model class
public class ViewModelBase {
public string MyText { get; set; }
}
// Some other view model
public class MyOtherViewModel : ViewModelBase {
// other properties
}
// In the _Layout view, implement the base class
#model ViewModelBase
...
#Html.DisplayFor(m => m.MyText)
...
#RenderBody()
...
This way, your _Layout view can work with all the properties of the ViewModelbase class, while whatever view is rendered after that still will have the properties of their child view model - here MyOtherViewModel - available.
Hope that helps!
On a side note, I would not recommend an extended use of ViewBags for passing data to your view in MVC, simply because it has a very low maintainability compared to other methods, due to it not being strongly typed. In my opition, ViewBags does not have any real benefits compared to - for instance - using viewmodels.
As an alternative, you can create a base controller inheriting from the controller and assigning the ViewBag variable to your list in the base controller constructor, like so
public class BaseController : Controller {
public BaseController(){
ViewBag.Order = SELECT FROM DB -> ADD TO LIST
}
}
Then in other controllers, inherit from the base controller like so
public class HomeController : BaseController {
}
This then allows you to assign the view bag for each controller request and the view bag variable will be available in the Layout View.
I myself have used this technique to show a list of car makes that is shown throughout the website.
As a suggestion though, I would implement some form of caching, to save constant calls to the back end.
However if you want to keep the functionality in the home controller you will need to restructure it so that it can be used via an Ajax call using jQuery/Javascript as #counterflux has suggested in their second point

Reusing a view in another view with parameters in ASP.NET MVC

Being rather new to ASP.NET MVC, I am already seeing some benefits of it for code reuse and maintainability. When asking a previous question on routing, I had another idea for my example, but unsure how to implement it.
Question: How can I reuse my issue view and controller in separate pages, as well as having parameters for certain things (like how many issues to display)?
Example: Say in my web application I want to show a list of issues. These issues are grouped by projects. So if you go to www.example.com/projectname/issues, you would see a list of issues for that project, and if you went to www.example.com/issues, you would see all issues for all projects. What I would like to do is that if you go to www.example.com/projectname, you would see some info about that project, including the last 10 issues submitted.
How can I reuse this issue code? I see I have the option for Partial View, but when I implemented it, I was unsure how to route any code to it. In Web Forms, you could make a ASP.net control, set some parameters in the code behind, and then embed this control elsewhere.
Some of the examples I have found so far either lack a complete implementation (goiing beyond just adding some HTMl in other pages), look like older MVC code that doesn't seem to work for me in ASP.NET MVC 3, and lack allowing me to set paramaters and showing this type of reuse.
My terminology may not be entirely correct here. If anything, I am trying to find the best (read MVC) way to replicate something like ASP.net Web Forms User Controls. As in, reusing my 'issues' code (HTML and C#) on both a 'master' issues list, as well as an issues 'widget' if you will
Skip the temptation write code in the view that goes out and accesses data on it's own. That includes using built-in functions like RenderAction. Even though RenderAction "goes back" to execute another controller it doesn't mean the view isn't taking an action on its own, which arguably breaks the MVC approach where views are supposed to do nothing and the model is supposed to contain everything the view needs.
Instead what you could do is send back a model for your issue list page(s) which contains a property containing the issues list:
public class IssueListModel {
public List<Issue> Issues { get; set; }
}
Populate it in your issue list action:
public ActionResult IssueList(string projectName) // projectName may be null
{
var issueListModel = new IssueListModel();
issueListModel.Issues = SomeRepository.GetIssues(projectName); // whatever you need to send...
return View(issueListModel);
}
Then on your list pages you could loop through it:
#foreach (var issue in Model.Issues) {
<div>#issue.IssueName</div>
}
Or you could send the Issues collection down to a partial view:
#Html.RenderPartial("IssueList", Model.Issues)
You can type your partial view to expect List as the model:
#model List<MyProject.Models.Issue>
... and then loop through it in the partial view, this time doing a foreach on the model itself:
#foreach (var issue in Model) {
<div>#issue.IssueName</div>
}
Then what you can do is make a separate model for your project detail view which also contains a property containing Issues:
public class ProjectDetailModel {
public Project Project { get; set; }
public List<Issue> Issues { get; set; }
public string Whatever { get; set; }
}
In the controller you can populate this List using the same function that you would populate in your lists controller:
public ActionResult ProjectDetail(string projectName)
{
var projectDetailModel = new ProjectDetailModel();
projectDetailModel.Issues = SomeRepository.GetIssues(projectName, 10); // whatever you need to send
return View(projectDetailModel);
}
Then you can re-use the same exact partial view on your ProjectDetail view:
#Html.RenderPartial("IssueList", Model.Issues)
A long answer but I hope this is what you were looking for!
If you want to re-use presentation logic only, you can use partial view. If you want to re-use also controller's logic, you have to use child action combined with partial view.
Create a controller
public class IssuesController : Controller
{
[ChildActionOnly]
public PartialViewResult List(string projectName, int issueCount = 0)
{
IEnumerable<Issue> issueList = new List<Issue>();
// Here load appropriate issues into issueList
return PartialView(issueList);
}
}
Do not forget also to create appropriate partial view named List within the folder Issues.
Finally use this line within your project view
#{ Html.RenderAction("List", "Issues", new { projectName = "Px", issueCount = 10 } ); }
and this line within your issue list view
#{ Html.RenderAction("List", "Issues", new { projectName = "Px" } ); }
In your controller method return the view as named rather than just View()
ie...
public ViewResult IssueView1()
{ return View("Issue");}
public ViewResult IssueView2()
{return View("Issue");}

What do when strong typing with two types needed

I'm quite newbie to mvc and as I'm designing some blog-like web app I want to know one thing from more experianced. The problem is that I have a view of a blog article and box for typing comments for it. The perfect, abstract solution would be to type this view strongly with a class of both article and comment, but as we know, it's not possible.
What will be the best way to handle this case? As it's a View of article, this should typed with article class. On the other hand - it will be easier to type it with comment class and pass article fields with ViewBag. I don't find it right as logically it shouldn't be typed with a class, that isn't directly represented here (article is the main one).
What are other possible solution to add and validate comments easily and do it in a clear way?
Thanks in advance.
Edit: main "problem" is not to handle list of existing comments, but to add new one - can I use with ViewModel FormCollection in this case easily on the controller side?
You would use a ViewModel in this instance. Something like this:
public class Article
{
public string Text { get; set; }
}
public class Comment
{
public string Text { get; set; }
}
public class BlogPostViewModel
{
public Article Article { get; set; }
public IEnumerable<Comment> Comments { get; set; }
}
ViewModel: the connecting object between a domain model (or models) [in your case Comment and Article] and a View [in your case, your blog post view].
Your View would look something like this:
#model BlogPostViewModel
#* you can reference Model.Article, as well as Model.Comments here *#
Here is a good working example of ASP.NET MVC View Model Patterns.
Edit: and then for an Action method to receive your POSTed data:
[HttpPost]
public ActionResult YourBlogView(BlogPostViewModel bpvm)
{
// here you have access to the binded model
// i.e. bpvm.Article
}
The perfect, abstract solution would be to type this view strongly with a class of both article and comment, but as we know, it's not possible.
But it is possible. And it's called a ViewModel.
You seem to be fixated a little on the CRUD model. It looks mainstream but that's because it's the only approach that lends itself to code-generation and templating.
To get a better perspective, take a look at the AccountModels in the standard MVC templates.
Use a new model:
public class ArticleAndCommentsModel {
public ArticleModel Article { get; set; }
public IEnumerable<CommentModel> Comments { get; set; }
}
An your view will inherit ViewPage<ArticleAndCommentsModel>.
I Can see two options
Create a viewmodel with the article and a comments collection
Render the comment section as a separate view (on the same page)
An alternative to the ViewModel approach suggested by the others would be to put the Article in the ViewBag and reference it from there and let the Comment be the page's model. This assumes the user doesn't modify the article.

asp.net mvc 2 multiple partial view

I have a contoller that renders 3 different views. But I also have a common part (div) for every view. I thought that I can create an UserControl with own controller and include that control on my views (New controller and view as controll).
How should I use that UserControl? Should it be a partial view?
Or different approach - can I have multiple partial views on one page?
I've been searching the web for the last view days and haven't found working solution that suits me. Also I want to use Strongly Typed views/data.
Cheers
You should use a partial view. Then you call <% Html.PartialRender("MyCommonControl", Model); %> in the 3-4 views to render the common section (like a menu or something).
This way you can strongly type the partial view and pass the model (like in the above example) or part of the model to it that is relevant.
UserControls are a ASP.NET Forms paradigm really, you should use partial views because they use the same MVC View Engine.
Update
If you place the PartialView in /Views/Home it'll only be accessible to the HomeController You want to put it in /Views/Common to make it accessible to ALL controllers.
You should also make a Generic ViewModel for The data that control needs, and make it a sub-component of the models for each Controller:
Eg:
class CommonSectionViewModel
{
public string Data { get; set; } // Just Example Data
public int Count { get; set; }
}
class ProductsModel
{
public CommonSectionViewModel CommonData { get; set; }
// Other properties for a products models
}
class CompaniesModel
{
public CommonSectionViewModel CommonData { get; set; }
// Other properties for a company model
}
Then in your Views for your controllers you call the partial render like this:
<% Html.PartialView("MyCommonControl", Model.CommonData); %>
Note: You can override the control as well
Having the following files:
/Views/Common/MyCommonControl.ascx
/Views/Products/MyCommonControl.ascx
When you call .RenderPartial("MyCommonControl") from ProductsController #2 is used, and from any other controller, #1 is used. So you can override functionality for some controllers if you wish.

Passing data to Master Page in ASP.NET MVC

What is your way of passing data to Master Page (using ASP.NET MVC) without breaking MVC rules?
Personally, I prefer to code abstract controller (base controller) or base class which is passed to all views.
If you prefer your views to have strongly typed view data classes this might work for you. Other solutions are probably more correct but this is a nice balance between design and practicality IMHO.
The master page takes a strongly typed view data class containing only information relevant to it:
public class MasterViewData
{
public ICollection<string> Navigation { get; set; }
}
Each view using that master page takes a strongly typed view data class containing its information and deriving from the master pages view data:
public class IndexViewData : MasterViewData
{
public string Name { get; set; }
public float Price { get; set; }
}
Since I don't want individual controllers to know anything about putting together the master pages data I encapsulate that logic into a factory which is passed to each controller:
public interface IViewDataFactory
{
T Create<T>()
where T : MasterViewData, new()
}
public class ProductController : Controller
{
public ProductController(IViewDataFactory viewDataFactory)
...
public ActionResult Index()
{
var viewData = viewDataFactory.Create<ProductViewData>();
viewData.Name = "My product";
viewData.Price = 9.95;
return View("Index", viewData);
}
}
Inheritance matches the master to view relationship well but when it comes to rendering partials / user controls I will compose their view data into the pages view data, e.g.
public class IndexViewData : MasterViewData
{
public string Name { get; set; }
public float Price { get; set; }
public SubViewData SubViewData { get; set; }
}
<% Html.RenderPartial("Sub", Model.SubViewData); %>
This is example code only and is not intended to compile as is. Designed for ASP.Net MVC 1.0.
I prefer breaking off the data-driven pieces of the master view into partials and rendering them using Html.RenderAction. This has several distinct advantages over the popular view model inheritance approach:
Master view data is completely decoupled from "regular" view models. This is composition over inheritance and results in a more loosely coupled system that's easier to change.
Master view models are built up by a completely separate controller action. "Regular" actions don't need to worry about this, and there's no need for a view data factory, which seems overly complicated for my tastes.
If you happen to use a tool like AutoMapper to map your domain to your view models, you'll find it easier to configure because your view models will more closely resemble your domain models when they don't inherit master view data.
With separate action methods for master data, you can easily apply output caching to certain regions of the page. Typically master views contain data that changes less frequently than the main page content.
EDIT
Generic Error has provided a better answer below. Please read it!
Original Answer
Microsoft has actually posted an entry on the "official" way to handle this. This provides a step-by-step walk-through with an explanation of their reasoning.
In short, they recommend using an abstract controller class, but see for yourself.
Abstract controllers are a good idea, and I haven't found a better way. I'm interested to see what other people have done, as well.
I did some research and came across these two sites. Maybe they could help.
ASP.NET MVC Tip #31 – Passing Data to Master Pages and User Controls
Passing Data to Master Pages with ASP.NET MVC
I find that a common parent for all model objects you pass to the view is exceptionally useful.
There will always tend to be some common model properties between pages anyway.
The Request.Params object is mutable. It's pretty easy to add scalar values to it as part of the request processing cycle. From the view's perspective, that information could have been provided in the QueryString or FORM POST. hth
I thing that another good way could be to create Interface for view with some Property like ParentView of some interface, so you can use it both for controls which need a reference to the page(parent control) and for master views which should be accessed from views.
The other solutions lack elegance and take too long. I apologize for doing this very sad and impoverished thing almost an entire year later:
<script runat="server" type="text/C#">
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
MasterModel = SiteMasterViewData.Get(this.Context);
}
protected SiteMasterViewData MasterModel;
</script>
So clearly I have this static method Get() on SiteMasterViewData that returns SiteMasterViewData.

Categories