Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 108113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:51:38+00:00 2026-05-11T01:51:38+00:00

There’s no strongly typed View() method to return an ActionResult. So, suppose I have

  • 0

There’s no strongly typed View() method to return an ActionResult. So, suppose I have

class Edit : ViewPage<Frob> 

In my FrobController, I will do something like ‘return View(‘Edit’, someFrob);’. There’s no checking going on here, so I have to always manually synchronize the view and controller’s use of it. This is suboptimal, and I’m not aware of any built-in fixes.

I added this method to my controller base class:

public ActionResult ViewPage<V, M>(V view, M model)     where V : ViewPage<M>     where M : class {     return View(typeof(V).Name, model); } 

Note: The reason I take a view object that’s never used is because, AFAIK, there’s no way to get C#’s type inference to work otherwise. If I removed the view parameter, I’d need to specify V explicitly, which also means specifying M explicitly too… sigh.

So now, I can do this:

  return ViewPage(new Views.Frob.Edit(), myFrob); 

I’m specifying the exact view (no problem if it gets renamed), and myFrob is typechecked to be the right model type. The ugly side is that I new up a Edit. Alternatively, I could write:

  return ViewPage((Views.Frob.Edit)null, myFrob); 

One downside is that the the model must be an exact match. So with a ViewPage>, I cannot pass in a List. I thought this might work:

    public ActionResult ViewPage<V, M, T>(V view, T model)         where V : ViewPage<M>         where M : class          where T : M {         return View(typeof(V).Name, model);     } 

But C#’s type inference can’t figure it out. The other potential problem is that the name of the view type might not be the right name, as I think it can be overridden by attributes. But that’s an easy fix if I run into it.

Questions:

  1. How can I make this syntax cleaner?
  2. What downsides am I missing here?

Edit: With respect to the Controller knowing about the View, it only slightly does. The only thing it gets from the view is the Type, for which it grabs the name. So that’s equivalent to passing in the string name. And the strongly typed model, which must match or it’ll fail. So it doesn’t really know too much about the View — its just a trick to get the compiler to catch errors.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:51:39+00:00Added an answer on May 11, 2026 at 1:51 am

    The first problem I see is that you’ve now made your Controller aware of the View. That’s a line you should never cross.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is any way to set the generic type (T) of class in the
There is a variable, suppose item . It is an array holding two values
There are 3 methods as f1, f2 and f3. I want to return from
There are two vectors std :: vector and QVector . We have to check
There is a reason why this method doesn't work? -(void)textViewDidBeginEditing:(UITextView *)textView { textView.text =
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a view passing on information from a database: def serve_article(request, id): served_article
There is no view controller or any interfacebuilder, I want to build this with
There's IsAssignableFrom method returns a boolean value indicates if one type is assignable from
There's the thing : I have this Url Rewriting condition in my .htaccess :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.