Can I Create a Partial View with Controller - c#

Using ASP.net MVC3 with C#
I want to create a partial view that uses a single database record so I have the View down fine but what controller would I create to actually create the LINQ query or do I need to do it in the View file?
The partial will display a random ad and be on every page on the site.

As M.Babcock says, you really should keep the logic out of the View and into the Controller (or Model if appropriate).
I would pass the resulted data to the View either through a Model, or it little data then just pass it through the ViewBag (or ViewData).

Related

How to render a view into another one, after an ajax request in C# and ASP.NET MVC?

I'm new at programming using ASP.NET MVC with C#. My problem is the next, I have a form to do an advanced search, that send every field in an ajax request. In the controller I receive all information and then I save it into a SQL table. I can Access this search in another view by accessing the corresponding URL, for example searches/viewSearch/123. What I need is to render the view just after I saved the search in my table, in the same view where I have my form.
My recommendation is to use Partial Views. Move your table to a partial view, your view can then load that partial view via Ajax when you want to access the corresponding url. You can also use
#{
Html.RenderPartial("_yourpartialview");
}
instead of an ajax call.

How can we use one div one multiple places in View in Mvc

How can we use one div on multiple places in View in MVC.
Suppose I have declared one div and just call that div in multiple frames in Views razor in MVC.
Consider using of partial view for you requirement. So the div you want to show many place will be part of partial view with its own ViewDataDictionary.
Partial Views
A partial view enables you to define a view that will be rendered inside a parent view. Partial views are implemented as ASP.NET user controls (.ascx).
When a partial view is instantiated, it gets its own copy of the ViewDataDictionary object that is available to the parent view.
The partial view therefore has access to the data of the parent view. However, if the partial view updates the data, those updates affect only the partial view's ViewData object. The parent view's data is not changed.
Check this for more information
Or
Another way is using jQuery to get div and inject where it is required.
Create a partial view in Shared folder. Put your common HTML there in that partial view. Use #Html.Partial("_SharedPartialViewName") wherever you want that common html.
You can also learn implementation from this URL
http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC

Putting ui attributes on domain model

My web application currently has a domain model and a view model. Domain model is almost a representation of my database. So when I insert data into a table, i'm inserting my domain model object. My view model handles how I want to display my ui. So if i have a property on view model that has a drop down list, i give it a custom display attribute drop down list with an enum saying what kind of values to load ([DropDownList(Enums.Product)]. My view model also has display labels and calls specific editor templates for the different properties using UIHint. I feel like creating a view model is becoming repetitive. I'm wondering if there's any harm in just putting display attributes (uihint, display(name="blah")) right on my domain model so i can skip the view model unless specifically need it for something. The only other solution is to actually wite out the html so that i'm using Html.Editor("SomeProperty", Model.SomeProperty) instead of Html.EditorFor(x => x.SomeProperty). Any thoughts?

MVC Getting model from partial view

I am new to MVC and i was trying to covert one of my webform project.
I have a request page, depending on drop down i select, controls get populated.
There are 10 request types, so i considered using partial view. I will make an ajax request on select change event, and depending on what is selected, i will return the partial view, but when i submit the main page, how will i retrieve the model for partial view, can i retrieve model separately for main page and partial page.
Yu have no way to pass a model from the client side to the server for rendering a partial view. So, no, that's not possible.
As you want to deliver a different partial view depending on the selected item, you need to pass this information to the browser, so that it can decide which action has to invoke to render the view correspondeing to the selected item. To do so, you can add this information as the value of each element int the drop down list. This value can be something as simple as an id or as complex as a whole url with parameters which invoke the required view. (If you choose the url option, you can render it using the Url.Action url helper extension).
Then, in the code that handles the change event, you can recover the value of the selected item, and use it as a parameter for making the ajax call that will render the required partial view. (For example, if it's the url, you can use jQuery.get() or any of the other jQuery ajax methods with that url).
The ideal situation is that you can render the partial view without depending on the previous rednering of the main view. I.e. the ideal is that you can build the model for the partial by using the action and parameters received in the ajax call.
But, if you need some information that must be generated when rendering the main view, you can use can use TempData to store it when rendering the main view, and to retrieve it when the partial is rendered. (TempData or Session depenging on what yu exactly want to do).

MVC3 nested partial views submitt. how does it work?

I have started working with mvc3 pattern and am facing a problem.
The problem statement is some what as follows:
I have a partial view lets say MasterPartial which renders some content and is bound with a model named MasterPartialModel which contains all necessary data to display on that partial view. The MasterPartialView has nested partial views uptill n level and the nested child partial view that renders on demand within the MasterPartialView is bound with ChildPartialView.
Further more, the MasterPartial view has the form tag. Now my question is upon submit which model would i be receiving and upto my understanding it would be the model of the view which has the form declared within it. How would I know if a user makes a change in the data rendered by one of the childviews. How would I get models of the child view which were rendered on demand by the user, or simply how do we cater for this kind of a problem in MVC3 pattern?
Fixed the issue by working around the problem and by using jquery to go through all the loaded partial views. Collected all the values required filled an array of the type of a model and sent that array of models to the controller method for processing. :) jquery i awesome specially if coupled with mvc...

Categories

Resources