Showing posts with label validation of asp.net controls. Show all posts
Showing posts with label validation of asp.net controls. Show all posts

Wednesday, 22 May 2013

How to create a File Upload with File Validation in Asp.net MVC / How to Use the @Html.TextBoxFor() for File Upload ?

While Wonder how to validate the Upload control in a easy way, I find this solution as the best to do it, although lot of other ways are found.This is because you need not need to write much more code as a normal razor validation will solve the problem.


My View:


 <div id="dvUpload" class="hide fieldControlContainer">
                <div class="innerDivLeft">
                    <label class="blue">Upload Document:</label>
                </div>
                <div class="innerDivRight">
                      @Html.TextBoxFor(model => model.File, new { @class = "inputTextBox", type="file" })
                      @Html.ValidationMessageFor(model => model.File)
                </div>
 </div>

The magic lies with this line  @Html.TextBoxFor(model => model.File, new { @class = "inputTextBox", type="file" }), here it will be rendered as a file upload control rather than a text because we are over riding the default behavior of input tag rendering by specifying it as type="file".

My Controller:



 public ActionResult Index(HttpPostedFileBase file, MyModel model)
        {
                  //Codes written here.
        }


MyModel:

public class MyModel
{

       [Required(ErrorMessage = "Please select file")]     // this is for model validation
        public HttpPostedFileBase File { get; set; }

}

Now your file upload validation will be checked like any other normal control.

Thursday, 5 April 2012

Is it possible to make a radio button a required field? or Adding a required field to radio button


 yes, see the example below.

<asp:RadioButtonList runat="server" ID="radio">

                <%--Creaint Items to be display in Radio Button List--%>

                <asp:ListItem Value="0">Male</asp:ListItem>

                <asp:ListItem Value="1">Female</asp:ListItem>

            </asp:RadioButtonList>

            <%--Creating RequiredFieldValidator control to validate RadioButtonList that we have created--%>

            <asp:RequiredFieldValidator runat="server" ID="radRfv" ControlToValidate="radio"  errormessage="Select One option"></asp:RequiredFieldValidator>



Either you can use initialvalue property required filed validator. which is like  

 <asp:RequiredFieldValidator runat="server" ID="radRfv" ControlToValidate="radio"  initialvalue="0" errormessage="Select one option"></asp:RequiredFieldValidator>

For radio buttons:

Radio Buttons do not support the Required Field Validator. You can
either have a button selected by default or employ javascript. Here's a
simple example that forces a user to select either a Male or Female radio
button. There are plenty of variations of this on the Internet if you do a
little digging:

if ( ( document.myForm.gender[0].checked == false ) && (
document.myForm.gender[1].checked == false ) )
{
alert ( "Please select either Male or Female" );
valid = false;
}