.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Is it bad MVC practice to return a view of a different controller?

Posted By:      Posted Date: August 29, 2010    Points: 0   Category :ASP.Net
I came upon this when had problem trying to return a view of a different controller.  In controller1, I have view view1.In Action1 of controller2, I wanted to do something like:public ViewResult Action1(){     return View("/Controller1/View1");}but, found out, I couldn't do it.  So I searched and found this: http://stackoverflow.com/questions/879852/asp-net-mvc-display-a-view-from-another-controllerOne guy there responded and said that, for my case, it is possible to do things like below:return View("~/Views/Controller1/View1.aspx");But, please let me know if this is considered an acceptable MVC practice.  Is it OK, good, perfect to do so in the asp.net MVC realm?  Thanks, please share your two cents.

View Complete Post

More Related Resource Links

Model View Controller (MVC) for ASP.NET

Model View Controller (MVC) architectures, immediately wonder how the MVC pattern can be applied in an ASP.NET world. This article will demonstrate how the MVC pattern can be applied to an ASP.NET web application and still maintain the basic Microsoft supplied architecture.

General Controller to View logic question

Hello, I am currently learning the MVC2 Framework and I have a brief question in terms of my logic and if their are any flaws in it. I created a basic example in a couple of minutes that demonstrates the way I think about it. public ViewResult Index() { return View(); } public ViewResult Cookies() { return View(new Cookie()); } public ViewResult CookiesInput(Cookie cookie) { return View(cookie); }<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>I really like cookies</h2> Would you like a cookie? <% using (Html.BeginForm("Cookies", "Example")) { %> <p><input type="submit" value="Yes" /></p> <% } %> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Cookies</h2> <% using (Html.BeginForm("CookiesInput", "Example")) { %> <%= Html.EditorForModel() %> <p><input type="submit" value="Gimme" /></p> <% } %> </asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainConten

Passing return value from a Web Service to a label in a Form View

I am using JavaScript to call a web service. The JS is correctly receiving and passing the web service information. At the moment, the web service returns a country name as a string to a JavaScript alert pop up box as follows:ret = CountryRegionsWebService.GetTranslatedCountryShortName(ddl1_value, ddl2_value, OnComplete, OnTimeOut, OnError);function OnComplete(args) { alert(args); } I want to change this so that web service returns the country name to a label inside an insert item template of a form view. Is there a way to pass in the label variable to the OnComplete function signature and still account for the variable args?, such as: Server Side Declaration of JavaScript ddl2.Attributes.Add("onChange", "JavaScript:OnComplete('" + lab5.ClientID + "', '" + "args" + "');"); Client Side JavaScript function OnComplete(lab5_id, args) { var lab5 = document.getElementById(lab5_id); lab5.innerHTML = args; }  

Passing objects between View and Controller

 I would like to pass List<SelectListItem> (not selected item but the whole "List" object) back to controller.for example in my GET controller i would have thisViewData["list"] = some select list from repository;then on post I would like to get the list back from view..pretty much i'm trying to use ViewData["list"] as storage..this way I would use ajax to remove or add items to/from the list.what's the best approach ?thanks

How to retrieve url from another view of controller by code


Dear all

I want to know url of View from another controller.  The following is structure







Now, I want to goto Index view of controller Home from view Login of controller Account

I know  I can use method this.RediectToAction("Index", "Home") to do it but I don't want to method.  It will fail to redirect when view or controller renamed.

Please help



Dear All,

I have

- controller (customer)

public class customer{

public string Name { get; set; }

public string sex {get;set;}


- model (customermodel)

public class customermodel{

public string Name { get; set; }

public string sex {get;set;}


- view (index.aspx)

so i want to transfers presentation model to view (use IList and IEnumberable), how can i do? after i want to display data from database, how can i do ?



problem when passing data from view back to controller?


I have following situation - I am pasing user info object from Controller to View. It contains GUID UserID,  which i dont want to be seen on page.

So I  removed every   Html.LabelFor(model => model.UserID), Html.TextBoxFor(model => model.UserID) etc... from generated View source.

And because of this when Html.BeginForm() returns that object back to Controller  all values is there but UserID is lost??

If I leave Html.LabelFor(model => model.UserID), Html.TextBoxFor(model => model.UserID) etc.. in View everything is fine.

But I dont want to show UserID?

Where is the problem here?

                <%= Html.LabelFor(model => model.C__User_Id) %>

data lost in transition from view->controller


MVC basics or looks like I am doing something terribly wrong, but have not slightly idea what is it?

Want to delete user from MVC Framework's Built in Security Module.

So first I define Delete() in AccountController:

        public ActionResult DeleteUser()


            User_Repository urp = new User_Repository();

            aspnet_Users currUser = urp.Get_AspNet_User(HttpContext.User.Identity.Name);

            return View(currUser);


and this works fine.


I added view (for delete) DeleteUser.aspx


<h3>Are you sure you want to delete this?</h3>




        <div class="display-label">UserId</div>

        <div class="display-field"><%= Html.Encode(Model.UserId) %></div>


        <div class="display-label">UserName</div>

        <div class="display-field"><%= Html.Encode(Model.UserName) %>&

How to create a View to return all multiple child fields in one record?


I have an application where the users can define up to 5 extra fields for a particular table (e.g. tblMaster). The custom data is all held in one table (e.g. tblCustom) so there could be up to 5 records in tblCustom for each tblMaster record. In my application I build a select statement based on the number of custom fields so that I can get a single row of data for each tblMaster record (sample below). I am now adding new features to the application and I want to expose an SQL View to the users for a report writer that will achieve the same result but still cater for the variable number of custom fields. I realise this needs to use probably a RIGHT OUTER JOIN so that NULL is returned if there is no matching field but I can't work it out. If I had five separate files it would be easy as each one would be a RIGHT OUTER JOIN for each. I could build 6 views (e.g. with no custom fields, with one custom field, with two custom fields etc) but that is not very efficient. I don't particulaly want to restructure the database as there would be a lot of work and retesting.
Any help would be very welcome!

SELECT tblProcess.ProcessNumber, tblProcess.Process, tblProcess.Risk, tblProcess.MRR, tblProcess.Version, tblProcess.LastChange, tblProcess.SecurityLevel, 

How to Pass data(not the part of model) from View to Controller?


How Can I Pass data(not the part of model) to Controller from View?
View Code

<%= Html.ActionLink(item.Name, "ActionName", "ControllerName", new { companyID = Html.Encode("abc"), ID = Html.Encode("xyz") })%>

Controller Code

public ActionResult ActionName(string companyID , string ID)

Any idea?

Problem adding a View to a Controller in VS 2010 Express


Hi all,

I am very new to ASP.Net Forums and just starting up with MVC2.

For that, I was trying to build MVCMusicStore Application based upon the guidelines from ASP.Net site.

But when I am trying to add a view from controller or from Solution Explorer. VS stops responding.

I am using Windows 7 Enterprise as an OS and Microsoft Visual Web Developer 2010

Is it a problem related to installation or something else???

Please help me out... :(

Invalid Password message on View From COntroller


If the password is invalid, how can I show "Invalid Password"on view ?

   <% Html.EnableClientValidation(); %>
   <% using (Html.BeginForm("Submit", "MyController", FormMethod.Post))
   <%= Html.ValidationSummary(true) %>
   <div><b><%= Html.LabelFor(x => x.UserModel.Password)%>:  </b><%= Html.PasswordFor(x => x.UserModel.Password)%>
   <%= Html.ValidationMessageFor(x => x.UserModel.Password)%>
    <input type="submit" name="submitButton" value="Accept"   />
    <% } %>


 bool isAuthenticate = IsAuthenticate(...);
    if (!isAuthenticate)
        return View("MyPasswordView", UserModel);



Deleting a Controller Action and its View in one step


Since a Controller Action and its View are associated with each other I would think that there would be a way to delete both in one step.

Is that possible? 

Need an MVC Guru !!! Best Practice to have multiple Strongly Type Models ...inside a single View ??


So far I've seen demos of MVC that have simple pages, one for login, one for register, one for listing a table ...

In order to understand better the transition for WebForms developers into MVC patterns, I would like to understand how can I put together multiple CONTROLS (like the one provided by the default MVC project that VS2010 creates for us ...and make it work.

OK so as an example, imagine we want to change the LogonUserControl.ascx to allow us to login (so to show UserName and Password textboxes and Logon button) from the Home Page.

1) We update the Control as follow:

2) We update the HomeController.cs file to handle the Logon Button Post event as follow:


We needed to move some code from AccountController to HomeController in order to get the control to work under the Index view. So ...with little luck it seems to work !!!

Now the problem happens when you navigate to the Register view, considering that now we are not any longer on the HomeController "domain" but we are under AccountController.

Register page shows perfectly and

Injecting a Script into the View from the Controller


I want to Injecting a Script into the View from the Controller on postback. How do you do it. Is there a RegisterClientScript equivalent?

Return list of items to a controller method


I have these tables in my database - Presses, Paper, and PressPaperSpeeds:

PressID (PK)

PaperID (PK)

PressPaperSpeedID (PK)
PressID (FK)

Model View Controller - MVC Framework

This article furnishes the information about the Model View Controller (MVC) and the overview of Asp.Net MVC framework . All the basic information about MVC is covered in this article.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend