Stepper form data - c#

I am new to dotnet core mvc. So I am asking a suggestion. I have 2 stepper form. I need to get data from this two form , do some validation and finally save this to db. How can I make view and get data from view to controller ? Should I make two view page and two controller ? Or one will be enough ?
Way to achieve this feature

You can create two separate view pages and two corresponding action methods in the controller (one for each step of the form). You can use TempData or Session to store data. Then, in the last action method, you can perform all the necessary validations and save the data to your database. Alternatively, you can use a single view page and use javascript to hide/show different parts of the form based on the current step. Finally, you can use a single action method in the controller to handle the submission and processing of the entire form data.
Tell me if this helps :)

Related

How to return an object from controller to View which may not be model

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

Controller calling itself and passing a value

How to pass data from the controller to itself?
Consider this example:
I have a page that consists of two parts: (1) a simple html form with a couple of text boxes and a submit button and (2) a table that is updated when the button from part (1) is pressed. When it happens, the data from the form has to be appended to the end of the table.
As I see it, there should exist a List of objects. Every time the button is pressed, the controller is called with two parameters: the old list of objects and textbox values. Then, the controller generates the object, adds it to the list and passes the new list to the view. The view is rendered with the new data and rows are successfully added to the table.
However, that requires reloading of the page and that feels kinda wrong.
The problem is, that there is no static object that can contain the list permanently, or at least that exists during these controller-self-calls. If there was such, I would not have to pass the whole list (which, as I said, I can't even do) but just new textbox values.
I have heard that partial views can solve the problem, but I can't see how.
What can I do?
For starters, as you said you'd like to achieve this without javascript, I see no way of avoiding: reloading of the page and that feels kinda wrong.
Not sure as well how partial views will make things work since they're rendered from your main view and require the same or part of your model, so you'll need to have that data there.
You have to get the information back from the controller, and the controller must get this information somehow so as I see it, these are your options:
Keep part (2) inside your form, thus making both parts available when you hit the controller. Model will get populated with the values you need and then the data is available for you when you're back at the View.
Keep a hidden input field inside part (1) containing the data you require to create the list. It's similar in concept to option #1 but I don't like this method too much, you'll have to do some parsing on that input field and this is not very elegant.
You could also try use Session or database but I think the latter is an overkill and a hit on performance so I wouldn't go with that.
If you don't have a database backing the form data, you could use Session data to hold the List.
In the controller, do something like this:
[HttpPost]
public ActionResult AddToList(object newObject)
{
var list = Sesssion["List"] as List<Object>;
if (list == null) {
list = new List<Object>();
Session["List"] = list;
}
list.add(newObject);
return View(list); // Assuming the view is a strong-typed view with List<Object> as model
}
As for partial views, they do not alone solve the problem of reloading the page. The solve the problem of a reusable, self-containing component of the page. If you don't like the reload of the page, you can use partial views together with Ajax calls though, in order to refetch the table whenever a new item is added. Here is an example

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).

General concept regarding dynamic textboxes on the fly

I am working on a project which has a requirement to build "pages" on the fly. A page can consist of various controls like textboxes, checkbox etc. Currently when the user wants to add a new textbox I make a ajax request and render partial view and return the HTML and show it on client side. This works but I also want to handle the data properly when these dynamic controls are filled up by user. In a way if I am not wrong I need to be able to make array of HTML controls. Now if we give static List to our view and generate textboxes using Html.TextboxFor we see that the name generated is something:
[0].FruitName
[1].FruitName
[2].FruitName
How do I handle this index part when making a Jquery Ajax request so that I always get the correct indexes and render it on client.
If anybody has any better solution than making ajax request then also please let me know. I need to handle the dynamic rendering of HTML controls and also access their values properly when posted back to server.
Take a look at Non-Sequential Indices at http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx.
He introduced a helper method to generate it as well.
Also, I think you can just pass an index with your ajax call which then gets passed from Controller to your partial view and use it to generate a proper indexed TextBox.
Update:
I've asked a very similar question at Submit javascript dynamically added elements to controller method like Stackoverflow

ASP.NET MVC: Do I really have to create one view per action?

If I have a fairly crud-based area of my app, do I really have to create separate "Create" and "Edit" views? The HTML will be practically the same. I want an "Edit" and "Create" action to both render a "Show.aspx" view, but certainly Resharper 5 is complaining about there being no "Show" action.
What's the best practice?
There are alternatives. Basically off the top of my head you have three options.
You could either make a user control and have very lightweight edit and create pages.
If you are using ASP.MVC 2 you can capture the layouts as attributes on your view model and use the new template helpers DisplayFor and in your edit / create case EditorFor / EditorForModel.
You can specify a view name on the call to View from your controller action.
You don't "have" to do anything. MVC is based on conventions, which are valuable, but these are not technically required. In your case, I think it's more important to avoid redundant code.
You might consider having only an "Update" action and an Update.aspx view (form) to go with it.
Use the same form for both creating and updating. The only difference is, when creating, the form won't have an object ID.
After submitting, if the Update action sees an ID, it loads the object. If not, it instantiates a new one. Then just update the properties from the form, and commit (save).
So, one action and one view. Less code and it keeps convention.
You can specify which view you want the controller method to use, so there is no strict requirement for having two different views.
If your Add and Edit views look exactly the same, but you want to make it clear to the user whether they are adding or editing, you can simply push a different title into the ViewData, and display it in the shared view.
You can also put views in the "shared" folder, or create .ASCX partials that can be shared.
Go for it. It makes perfect sense. View should always be set up for front-end developers convenience, as controllers can pass data to any view, without any extra effort. You shouldn't feel restricted to do something, just because ReSharper says so.

Categories

Resources