Don't render part of Model form Django - django-forms

I take some form choices from database by using ModelChoiceField in the form.
But i don't want to render select in form template if ModelChoiceField return empty QuerySet. How to do this?

Related

Edit Redux Form with FieldArray

I have create a Redux Form with FieldArray as mentioned in its example, where you can add multiple Member and Hobbies and Submit the Form.
https://redux-form.com/8.1.0/examples/fieldarrays/
Can we Edit the same Form by accessing the form from database? The Edit form should display all the fields and Button I have created before ?
Please advise how different sub-component(which is created using FieldArray ) will populate the values.
Thank you
You can reuse the same form for editing.
A field array is mapped into redux as array of objects or array of strings (depending how you do the fields.push).
If you provide that exact format to the initialValues prop when you mount the redux form, it will populate the FieldArray accordingly

Sitecore MVC multi step form

I am developing multi step form with sitecore and mvc. In each step I need the sitecore context and I am using controller rendering for the same. Now as sitecore works I need to add the controller name and action for my rendering so basically I can only use one controller action for each rendering.
In my case I have used the same action name but different parameters type for each step in multi step form. Ideally I would like to have different controller action name for each step within multi step form. Can anyone please help me with that?
Thanks
I'm not sure I understand the question completely. Are you trying to use a single .cshtml file for all the steps? If so, you can define multiple controller renderings for the same View in the Sitecore CMS. In the renderings section, define each step of your multi-step form as separate controller renderings. You can use the same controller for each, but specify a different action.
In your controller logic, you can specify to return the same View, but pass different properties into your Model object.
For example, you might do something like this:
public ActionResult Step2(){
var context = RenderingContext.Current.PageContext.Item;
var otherParams = "SomethingForStep2";
var model = new MyModelObject(context, otherParams);
var view = this.View("Path/To/My/View", model);
return view;
}
In the example above, I assume you have defined some sort of Model object where you can pass in whatever parameters you need so that your view can use this to render.
If you are using different views, then you will just return a different view for each action, again passing in a model to the View to help it render.
If you are using AJAX (which I would assume) I would recommend the following:
Serve each view/step of your multi-step form using a unique HttpGet action in your controller
Represent each view/step of the form with a unique item and a unique view (assign the views to the items and use an empty Layout to enable the form/view to be rendered on its own)
Handle each form's post action with a unique HttpPost action in your controller
From the HttpPost action, return a JSON object which can be handled client-side. Include in this result properties such as success, failure, validation errors and the next step of the form to display (a URL for the following AJAX request - call this property NextStepUrl)
In your client-side JavaScript have a function to handle the JSON result object returned by your controller's POST action. If successful read the NextStepUrl property then use this to perform an AJAX GET request for the next step of the form
This approach means that on each GET request you will have a valid RenderingContext from which to read your controller rendering's datasource.
If you need to have the RenderingContext during your HttpPost actions you can simply include a hidden field with the ID of the datasource item and read this in the HttpPost action, or you can use some custom model binding to achieve the same thing in a slightly cleaner way.

Create a Web Grid using Javascript

Can we create a WEB GRID dynamically using JAVASCRIPT ?
I am using MVC 3 and Razor as my view.
On one of my Razor view I have a drop down box what I want is to create a web grid dynamically on selection of a drop down I get the data depending on the value selected from the drop down box.
Please help me on this.
I had to use ajax for this, where I had called a method using ajax and my method (present in the controller) returned a partial view.
My partial view had the grid I needed to show. and in the method (present in the controller) I had passed value from the dropdown box using ajax, and using this value I had queried my database and sent that model to the partial view.

Using the HTML.DropDownList helper with view model and Ajax

I'm trying to use a html.dropDownList helper with a strongly typed view model with ajax. I can't the post the code because of the nature of the project.
Here basically what I'm doing...
loading a mvc view via a strongly type view model
clicking a button which does an ajax post to a controller method
using the TryUpdateModel to parse the view model
processing the request
rendering a parital view for the ajax request
According to the article listed below, the problem is that "ASP.NET MVC assumes that if you’re rendering a View in response to an HTTP POST, and you’re using the Html Helpers, then you are most likely to be 'redisplaying a form' that has failed validation."
http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx
Instead of "redisplaying the same form value", I need the html.dropDownList to be set to the same value in the view model.
Does anyone know of any custom dropDownList helpers or have any ideas of how to achieve this?
Things I've already tried/considered
per the blog, manually removing the modelstate item...didn't work - didn't pick up the value in the view model - just defaulted to the first item in dropdown list
considered just writing a regular select list...but this is sloppy and cumbersome since I'm rending multiple select lists in a loop
writing my own custom dropDownList helper...wanted to avoid reinventing the wheel
Thanks in advance
Its not fully clear to me what your problem exactly is, but I've had a similar problem. I used the Html.DropDownListFor(, SelectListItem[]) helper. At postback it sets the value to the choosen one. Your postback view doesn't require to have all the fields of the original model.
#Html.DropDownListFor(model => model.SelectedValue, MyModels.DropDownSelectables());
Here I want the selected value as the model.SelectedValue variable and within my (seperate) model I made a array of selectlistitems. The rest is automagic.
Hope it helps, D

passing checkbox and textbox values from a view form to a controller action

I have a view with a form..this form has a textbox and a checkbox in it.
i also have a submit button in the form which points to an action in a controller.
my question is..how can i pass the values in the textboxes and the checked state of the checkboxes to the controller action?
the textboxes and checkboxes are not tied to a model.
thanks
You have two options.
The best option would be to create a ViewModel. This model doesn't need to have any logic or anything behind it, just a public get/set for the properties. This means you can also use the MVC validation, helpers and binding on the values and makes it easier to work with.
The alternative is for your action to accept a FormCollection, this is basically a dictionary with the form values, the keys being the name attribute on your form elements.

Resources