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).
Related
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.
Here's my scenario:
I need to create a page such that I have a view containing a DropDownList, then another (partial?) view beneath it that changes depending on what was selected in the DropDownList. I know how to code the DropDownList and make it work with the controllers, but I'm pretty stumped on how to achieve my goal.
The view that will change based on the DropDown has its own controller with CRUD operations (they contain grids). I should add that i'm using a shared view that contains a sort of template for a grid. There's lots of grids, but one view, and a controller for each grid. So I can't just call in the view as a partial view, as it would have no data. I'm essentially needing to call the controller for each grid, which then renders the view containing the grid.
So how can I do this?
you can use partial views to render your views, hence you can use Ajax to update the contents of any element or part of the rendered views dynamically.
you may use jquery to get the selected item value from the drop down list
I was wondering if it is possible to modify a view HTML before sending it to the browser.
I wanted to create a custom tag compiler where i can insert a simple tag as <my-parsing-tag></my-parsing-tag> on the view and replace it for some specific HTML.
I'm already using OnActionExecuting and OnActionExecuted filters to execute some actions on the context (Change ViewBags, View names, Sessions, etc.), i also tried to do it there but i couldn't find the correct place to get the HTML, well i don't even know if it's possible to do so.
Is it possible or i would need to store my views HTML on the database to accomplish what i need ?
EDIT
As #Juan asked, why i need it:
I'm working with a call to action system where the user can place some specific modal campaigns on the page he wants just using those simple tags or selecting the page that will display it.
After that i will append the selected HTML to the view before sending it to the user. This system is intended for users that can't work editing the views since they don't work with HTML.
EDIT 2
After some research i have tried to implement a custom RazorView, the code is here with the Index View HTML, but now i have two problems:
The first one is that my Index View has some HTML that is coming from the database and is placed there using vars on my ViewModel and instead of the call to action HTML being placed at the end of my Index View, it's being placed before the ViewModel vars. The second problem is that the HTML is being duplicated instead of replaced. Here is an image of how the result looks like:
http://imgur.com/a/elul1
You could use an HtmlHelper extension for this:
http://tech.trailmax.info/2012/08/creating-custom-html-helper-in-mvc3/
I would suggest the following:
Define a container in your template (layout most likely) that will receive any content the user decides to "drop" into it via the admin panel.
You let the view know there is something to display via the ViewBag.
The view uses information you passed in order to render the desired content.
How it renders is where the HTMLHelper extensions come in. You could create an extension method which renders partial views based on the information you pass to it or maybe a set of extension methods that you call selectively based on the desired widget.
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
I am very new to MVC and I am not too conversant with the best practices here. I am facing a design issue which may be common or uncommon to newbies like me. My problem is the following:
I have a page with two parts in it.
Grid Control (with Employee basic info in it)
Employee Details (When someone clicks on Grid row, it loads all the details about the employee)
I am using KendoGrid and it is getting all its data from an ActionMethod from my controller.
Now, when I click on the row, I have the following options:
I call some ActionMethod in Controller and return all the Details
Should I use partial View with a separate model so that ActionMethod in response calls RenderPartialView()?
Should I NOT create a partial View, have Actionmethod return JSON and parse it in the Model?
3.1 If I go for this option then would the JSON be part of model?
3.2 If it is not going to be part of model, how can I use JSON to render the View?
Or probably I am missing something basic here?
Yes you can use action method returning JsonResult. what needs to be done is keep the uielements you need to show on the click of grid row in the page itself. initiate a ajax call to the action method and on success update the values of the UI elements from the values received in JSON and make the entire DIV as visible which holds the total information.
Instead of passing entire HTML over the network I think you can opt for json.
An example you can find at following location
example