Link to home
Start Free TrialLog in
Avatar of nicedone
nicedone

asked on

mvc, model binding ,validation

I have an MVC application, my problem is that I am submitting a form to mvc controller with some validation

if i use below model binding works as expected but then i use jquery validation engine which requires to pass a class and eroor message in the input element ,if i use below model binding works but validation fails

           @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)


if i use input as below then, validation works properly but then model binding do not work.

<input class="validate[required]" type="text" data-errormessage-value-missing="You need to specify a title"/>

 how can i over come this problem, i tried passing new {@class="validate[required]"} in the EditorFor but it did not work.i listed my whole form as below with my controller action at very end.



here is my form in asp.net mvc project;

form

<form id="companyEntry"method="post" action="/Home/CompanyEntry" enctype="multipart/form-data">

    <fieldset>
        <legend class="editor-label">Please Enter Your Company's Product Details Briefly</legend>
          <div class="editor-label">
             
        </div>
        <div class="editor-field">

          <input class="validate[required]" type="text" data-errormessage-value-missing="You need to specify a title"/>
        </div>

        <div class="editor-label">
            Description of the company,products...
        </div>
       
        <div class="editor-field-body">
              @Html.EditorFor(model => model.Body,new { @class="text-body-field" })
            @Html.ValidationMessageFor(model => model.Body)
       @*     <textarea class="text-body-field"></textarea>*@
        </div>
        <div class="editor-label">
            Upload Image
        </div>
        <input class="validate[optional,custom[validateMIME[image/jpeg|image/png]]]]" type="file" name="file" id="file" /><br><br>
        <p>
            <input type="submit" value="Submit" />
        </p>
    </fieldset>

</form> 

Open in new window


Controller
  [HttpPost]
        public ActionResult CompanyEntry(HttpPostedFileBase file,Topic company)
        {
            if (file != null && file.ContentLength > 0)
                try
                {
                    string path = Path.Combine(Server.MapPath("~/Images"),
                                               Path.GetFileName(file.FileName));
                    file.SaveAs(path);
                    ViewBag.Message = "File uploaded successfully";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            else
            {
                ViewBag.Message = "You have not specified a file.";
            }
            return View();
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial