MVC3 Finding a control by its Name - asp.net-mvc-3

I have a C#.Net web app and I am trying to access one of the HTML/ASP Text Boxes in the Controller for the Edit View of my Proposal model. In a non-MVC app, I was able to do this using Control.ControlCollection.Find(). Is there an equivalent for a MVC3 project?

You ask for an equivalent of Control.ControlCollection.Find() in MVC?
In MVC your controller is not aware of controls.
The controller just receives data via parameters and returns data via the function result.
What do you want to do with the control in your controller code?
If you want to access the value, you should bind it to a parameter:
View:
<input name="MyControl" type="text" />
Controller:
public ActionResult MyAction(string MyControl) {
// MyControl contains the value of the input with name MyControl
}

The MVC pattern was designed to keep things separated.
The View has no knowledge of the controller at all
The Controller only knows that a view exists and what kind of data that it needs. It do not know how the data is render.
Hence, you can never get information about controls/tags in the view from the controller. You need to use javascript/jQuery in the view and invoke the proper action in the controller.

In an MVC-application you don't have controls like in a webform-application.
In MVC you collect your required data in the controller and pass it to the view.
Typicaly the view is a HTML-page with embedded code.
In opposite to controls in webforms which produce HTML and handles the post-backs in MVC you have to do all this manually. So you don't have controls with properties and events wich you can access easily in the controller and you have to handle all your posts with your own code.
Thats sounds as it is a lot of more work - and indeed it could be if you implement the behaviour of complex controls - but MVC applications are much better to maintain and you have 100% influence to the produced HTML.

Well probably i am late for this but it should help others in future...u can store ur value in hidden field in view and then access that value in controller by following code..
Request.Form["hfAnswerOrder"].ToString();
Point - hfAnswerOrder is the ID of the hidden field
My Control in cshtml page..
#Html.Hidden("hfAnswerOrder", Model.Answers.ToList()[0].AnswerOrder)

Related

ASP.NET Core populate model only once for all views

I'm new with ASP.Net Core (3.0 in this case) and I´m trying to create a menu that is visible on all views of a WebApplication, is created dynamically and must be populated only once. Below i explain the steps and try outs i did to reach the goal needed (if required i can share the code I'm using).
This is what i did:
In a simple way, using the "_Layout.cshtml" page, i created a static HTML menu and made all other views simply inherit that layout. So far, so good;
Next challenge comes from the fact that the menu items are dynamically created after a User has logged-in, which i managed to overcome by setting a ModelView inside a controller (HomeController.cs with Index action in this case), and then delivering it to the view. For this case works OK, because the default page is ~\Home\Index\, problem is when i change to a different view with a different controller, the menu has to be rendered again, and so i have to replicate the code (a problem dealt create a BaseController and BaseModel based on this post along side the OnActionExecuted to host the menu generating code)
Now, the biggest problem is the fact that i can only populate the menu once, after the user logs-in. Each time there is a redirect between different controllers/views (post-back of same controller/view works fine), the model is null inside the OnActionExecuted, I tried using ViewData, ViewBag, TemData, but all are null.
So, my question is, how to keep that specific data alive and shared, basically across all the views, and only gets populated once (after each user login) between redirects from different views?
I have been reading around and found several solutions besides the one i did, but i did not found any that could keep data alive throughout the user session the way I need:
ViewBag, ViewData and TempData
Can the shared layout view have a controller in ASP.NET MVC?
Pass data to layout that are common to all pages
To sum up, my flow at this moment, is like this:
User Logged-in
Redirect to default: ~\Home\Index
MenuModelView.cs for the menu gets built and HomeController.cs returns to Index.cshtml with the model attached to it.
Index.cshtml receives the populated ModelView and it uses _Layout.cshtml
The _Layout.cshtml builds the HTML tags for the menu based on the MenuModelView.cs data
User navigates to a different view and steps 3 to 5 are repeated from a specific controller/view
If you want to create a control that can be accessible in all pages without changing every controller, I strongly suggest creating a view component. For a view component has no relationship with your controller, but can access dependencies like database and full HTTP context.
For example, you want to build a custom nav menu, you can just create a view component named NavHeader
using Microsoft.AspNetCore.Mvc;
namespace YourProject.Views.Shared.Components.NavHeader
{
public class NavHeader : ViewComponent
{
public NavHeader(YourDbContext context)
{
// you can access your dependencies, like database.
}
public IViewComponentResult Invoke()
{
// your own logic. You can access HTTPContext here.
var model = new YourOwnModel();
return View(model);
}
}
}
And just call it in any view or layout.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
#*Render your component like this*#
<vc:nav-header></vc:nav-header>
</body>
For more details about view component, please reference:
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-3.1
https://anduin.aiursoft.com/post/2020/1/4/share-view-component-between-different-aspnet-core-web-project

MVC3 RenderPartial and the ViewBag

I'll try to keep this brief:
In the view
#Html.RenderPartial("myview", Model.SubModel,
new ViewDataDictionary()
{
{ "thing", Model.Thing }
})
In the myview we see that "thing" is avaiable, i.e. this produces the value of Model.Thing in the myview view:
#ViewBag.thing
Great! Simple
But I need to do something with thing (and thing can't be part of SubModel by the way), i.e. access this in my view engine or ideally in the controller, e.g.:
public ActionResult myview(SubModelType vm)
{
var thing = ViewBag.thing; // oh dear this doesnt exist.. but is there when the view is rendered
So my question is, if the ViewBag is available in the resulting myview, it must be being passed around somehow in the httpcontext, or in the controllercontext somewhere right? Does anyone know why it's not available in the controller but is in the view and how I might be able to access this?
Edit
Sorry I missed a vital point here! It's when "myview" is posted back to the controller, invoking an action called myview where I would expect the ViewBag rendered to myview to be available. But of course it's not, it's been used in "myview" and that's it. So if I want to use it in the myview action I'm going to need to store in the ViewBag in that view OR set some value of the viewmodel so that it can be posed back to the action.
Does that make sense?
Sorry I missed a vital point here! It's when "myview" is posted back
to the controller, invoking an action called myview where I would
expect the ViewBag rendered to myview to be available.
Oh no, you cannot be possibly expecting anything like that. That's not how ASP.NET MVC works. When you send an HTTP request to a controller action the default model binder will intercept this request and look in the posted values from the request and bind them to your action argument:
[HttpPost]
public ActionResult myview(SubModelType vm, ThingViewModel thing)
{
...
}
This obviously assumes that those thing values were part of the original request. So for example if you submit a form you should include corresponding fields inside this form that the model binder could use.
Think of it that way: an ASP.NET MVC controller action could be invoked from any client. For example from an iPhone application. And as you know there's no such notion as ViewBag in iOS. All that will happen is that the default model binder will look at the POSTed values and attempt to hydrate the view models that your action is taking as arguments.
If on the other hand you cannot make those thing values as part of the request (by including corresponding input fields in the form) you could only send the ID of this thin from a hidden field and then inside your controller action use this ID to query your underlying data-store to retrieve the thing from the same place you retrieved it initially when you first rendered this form.
Some people might also suggest you storing the Thing inside the Session and then reading the value back from the Session in your POST action. I am not from those people. It's an alternative approach though.

How to load a Razor view and render it on the fly?

I have a custom CMS built with ASP.NET WebForms (you can see it in action at Thought Results). Now I want to build it using ASP.NET MVC 3 (or even 4). I don't want to change the architecture that much, therefore, I need to dynamically load a Razor View, and dynamically run a Model Loader method, and give the model to the view dynamically, then render the view, and return the result rendered string, all done in server.
In ASP.NET WebForms, my code is:
string renderedString = "LatestArticles.ascx".LoadControl().GetReneredString();
Now, I'd like to be able to write a code line like:
string renderedString =
"LatestArticles.cshtml".LoadView().BindModel("ModelBinderMethodName").Render();
I know about many questions about rendering a view (view to string), but I didn't find what I want.
You may checkout RazorEngine.

ASP.NET MVC Redirect to action without a physical redirect

Are it in any way possible to execute another action in the same on in another controller from an action, action filter or in any other way without doing a physical redirect.
The reason for this is that I have a dynamic paging system, where the user will load a url, for an example
/1/some-page-title
This url is maped to the controller “Home” and the action “Element”, this action will then load a row from the database where the element id is “1”. Depending from the data on the element from the database will the page be rendered as a contact form, an image gallery and so on.
Now I could map the paths so
/Page/1/some-title/ will render a normal html page,
/Contact/1/some-title/ will render a contact form
/Gallery/1/some-title/ will render a gallery
But I would prefer the paths to be simple.
There are problems with this answer, it's been a long time since I did anything thing with ASP MVC, so I'm not actually aware of what the problems are.
Unfortunately I can't delete an accepted answer.
So, I'm striking through the answer as it was, if you can actually answer this, or make it better, please do so.
Yes, very simple really :)
Say you're in controller C action A. You want to "redirect" to controller B action Z, just call the other controller action from the current one, returning it's result.
public ActionResult A()
{
return B.Z()
}
You may be looking for Html.RenderAction or Html.Action. However, these are used in the view and not the controller.

Help with understanding what goes in Controller vs. View in MVC

I'm new to MVC and I'm introducing myself through the Codeigniter framework. As a practice application I'm creating a simple blog application that gives the ability to create, view, edit, and delete posts.
For the "edit post" page, I'm using the same view file as my "create new post" and simply populating the form fields with the data I'm getting from my model when the user is editing an old post.
This seems all fine and dandy but I'm a bit confused on what logic is stored in the Controller vs. View. I obviously need some logic that tells the form to pre-populate the form fields (if I'm editing a post) or to leave everything blank (if it's a new post). There's also other things such as the view page needs to know what title to display at the top of the page: "Create New Post" or "Edit Post".
Where does this logic go? Does it go in the View? Or does it go in the Controller? and Why? I started putting all that logic inside the view file because I've read that I should keep the controller code minimal, however I now have a big php code block at the top of my view file that's dealing with variables and I'm not sure if this is the correct approach.
Any advice would be much appreciated!
I obviously need some logic that tells the form to pre-populate the form fields (if I'm editing a post) or to leave everything blank (if it's a new post).
Check out the set_value() function in the Form Helper. You can do something like this in your view:
<input type="text" name="subject" value="<?php echo set_value('subject', $subject); ?>" size="50" />
If it's a new post, pass empty string as $subject when loading the View from the Controller.
If it's editing, pass the subject of the post that is being edited as $subject.
Also, if a user submits the form and there are errors and you need to reload the form, set_value() will return what the user just posted (i.e. $_POST['subject']).
There's also other things such as the view page needs to know what title to display at the top of the page: "Create New Post" or "Edit Post".
Just pass a variable named $page_title from your Controller to your View, and set the value accordingly. This is pretty common, especially when you start building reusable templates.
Where does this logic go? Does it go in the View? Or does it go in the Controller? and Why?
There should be almost no logic in the View. Maybe simple if/else statements if you must, and loops to iterate through arrays of data.
Even Controller should not have much logic in it. It is mainly responsible for acting as a middle-man between the Models, the Views and the libraries.
I've read that I should keep the controller code minimal
That has to do with Controllers vs. Models, not the Views. It is good practice to keep Controllers smaller, and put as much of the logic as possible into the Models and the libraries. Also referred to as "Skinny Controllers, Fat Models".

Resources