ASP.Net MVC Form binding with Model sample - asp.net-mvc

i am new in MVC and try to learning through googling & online tutorial.
i search Form binding with Model sample in MVC and got many sample code but i am not getting a sample code for a complete form where all kind of html controls are used like
checkbox
radiobutton
listbox control
drodownlist
textarea
hiddenfields
checkboxlist
radiobuttonlist etc
and all controls should be bind through model with server side and client side both kind of validation. form should have two textbox one dropdown, 3 checkboxes and 2 radio button, one listbox control and if possible provide guide line to work with checkboxlist & radiolist.
if anyone knows that kind of url then plzz inform me or if possible please give me a sample code of a complete form where all the above controls will be there with model binding and validation. thanks

in the Views there is a helper called Html you can use it in the view like #Html
there are all things that you wanted, for example
#Html.HiddenFor
#Html.RadioButtonFor
#Html.TextAreaFor
and so on
search for asp.net mvc Html helpers
another smaple
#Html.CheckBox("sameName")
#Html.CheckBox("sameName")
#Html.CheckBox("sameName")
#Html.CheckBox("sameName")
it will produce this html
<input type="checkbox" name="sameName" />
<input type="checkbox" name="sameName" />
<input type="checkbox" name="sameName" />
<input type="checkbox" name="sameName" />
take a look at this website

Have a look here:
It Explains,
Rendering a Form in ASP.NET MVC Using HTML Helpers
http://msdn.microsoft.com/en-us/library/dd410596%28v=vs.100%29.aspx
http://stephenwalther.com/archive/2009/03/03/chapter-6-understanding-html-helpers

Related

MVC-bind simple html textbox without use of html helper and Razor

I have one question regarding MVC (I am new to MVC)
My question is
In MVC with Razor for textbox we write like this
#Html.TextBoxFor(m=>m.name)
Now suppose I don't want to use Razor and html helper class. I want
simple html input, like this
<input type="text" name='#Model.name' value='#Model.name'>
Is the above possible somehow?
you can use
<input type="text" name='#Html.NameFor(x => x.name)' value='#Model.name'>

Why doesn't TextBoxFor include validation elements if called twice for the same model property?

Simple question... Here is an example of some razor code:
#Html.TextBoxFor(c => c.RevisedEstimate)
#Html.TextBoxFor(c => c.RevisedEstimate)
Here is how this renders:
<input data-val="true" data-val-number="The field Revised Estimate must be a number." id="RevisedEstimate" name="RevisedEstimate" type="text" value="0" />
<input id="RevisedEstimate" name="RevisedEstimate" type="text" value="0" />
The obvious question you ask is, "Why are you doing that?". The razor view is actually building client side detail-row templates that are used in KendoUI grids. There are two similar grids and we use the same viewmodel server side. We actually do provide the id element for the template so each field in each row ends up with a unique id.
Why does the second input element not have the data-val and data-val-number elements?
Off the top of my head knowing what the JS does in the background, it seems to do this to prevent conflicts. The JS looks for the elements with the data- attributes to do it's validation, along with other functions, so it could possibly pick the wrong one if there are multiple instances of it.
since we were generating HTML for use in a client side template what we did was just create a variable to hold the HTML generated by the helper, and then render out that code in the Views..
Something like:
#{
var revisedEstimateInput = Html.TextBoxFor(c => c.RevisedEstimate)
}
Then later in the view:
#(revisedEstimateInput)
...in as many places as needed. This way the validation and other metadata attributes were in place in our client templates and all the kenodUI validation worked correctly.

<asp:TextBox> Vs <input type="text"> Vs Html.TextBox

I am working in asp.net application, adding functionalities like form elements, validation and populating data from DB.
I can use ASP: controls but I am wondering is it possible to use HTML.TextBox or <input type="text">
If I can use <input type="text" or Html.TextBox what are the pros and cons of using them versus using
Forget about server side controls in ASP.NET MVC. Everything containing runat="server" is a big NO in ASP.NET MVC. Won't work in Razor anyways. There are no pros and cons. Server side controls should never be used so there's nothing to compare. Simply use HTML helpers such as Html.EditorFor and Html.TextBoxFor.
If you're using ASP.NET MVC, then either the manual typing (<input type="text" />) or the helper extension (Html.TextBoxFor(x => x.SomeProperty)) are going to be your best bet, and it really depends on what you are planning on doing. If the control you are making has no property on your element, it can be a lot easier to simply hand code the input tag.
Never use <asp:Checkbox ID="blah" runat="server" /> in an MVC application.

Struts2 : Using form action

I am working with a struts2 based application
Inside my JSP page for form submission is it mandatory to use s:form (Predefined struts component )
Because when i tried this way it worked (calling the Action class under struts.xml )
<s:form action="HelloWorld" >
<s:submit />
</s:form>
But When I tried to use normal form submission as shown
<form action="HelloWorld">
<input type="Submit"/>
</form>
It isn't working , it gave me 404 error .
please tell me is it mandatory to use and for data submission ??
A struts form action and an HTML tag form action are different. You can use a standard HTML form tag with struts if you create a struts specific URL for example (off the top of my head):
if using in multiple places, generate the url in and call like this -
<s:url id="myActionUrl" action="HelloWorld" />
<form action="<s:property value="%{myActionUrl}" />">
<input type="Submit"/>
</form>
or using in a single instance -
<form action="<s:url id="myActionUrl" action="HelloWorld" />">
<input type="Submit"/>
</form>
You can often look at the page source in your browser to see what Struts generates and recreate it manually like this. You will often end up using additional struts tags such as property to retrieve values from your value stack, but it is useful at times, for instance when generating JavaScript code dynamically.
No, it's not mandatory to use any of the S2 tags, but as Russell says, you need to duplicate the correct action URL.
You also need to be a little careful when mixing-and-matching S2 form tags with non-S2 HTML form tags, because the default S2 theme adds additional HTML markup to the page; they don't just create form tags--the default theme uses table tags to lay out the form.
You can use s:form for form and
<input type="Submit"/>
can be replaced by
<button type="submit"/>
It's not about which to use,it is what you want from it.Struts2 tags provides additional capabilities to the form.
Please go through below two links to get the diffrence
1] http://struts.apache.org/release/2.1.x/docs/form-tags.html
2] http://www.w3schools.com/tags/tag_form.asp
some facilities such as namespace,tooltip,tooltipIconPath and many are provided by struts2 tags.

Maintaing state in ASP.NET MVC when you're not using the UI helpers

I have a bunch of forms where its easier to type the entire html tags:
However, when I pass the Model to the View, it doesn't show the value. What am I missing here?
You will need to manually inject the values from ViewData. The helpers encapsulate this, but if you are writing straight HTML there's no way for ASP.NET MVC to know how to wire the values to the form elements.
<input type="text" id="Name" name="Name"
value="<%= ViewData.Model.Name %>" />

Resources