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

nicedoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:
Hi,

First of all, what you could do is to see if you use the razor syntax what would the output be, when you load the page. This would give you a hint of how to do a "manual" model binding.

Haven't done this for a while, but if i remember well, you needed to add id and name attributes, which would point to the model property.

For instance if your company model had a property named "CompanyName" you would need to have your input as:

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

Open in new window


Still i am not 100% sure, so better check out how your page source is modified when it renders when you use the razor syntax.

That would give you a hint of how to fix the model binding.

Now, in a similar way you can check out why using the following razor syntax does not work:

@Html.EditorFor(model => model.Body,new { @class="text-body-field validate[required]" })

Open in new window


I guess the above is what you try. Now let it render and check the page source. How does it differ from the one that works? This will give you a hint of what you need to do.

I know this is more a hint of how to solve your problem rather than a solution itself, but please post what you get after doing what i suggested, and i can help you more, if you have not figured it out yet.

Giannis
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.