0

I want to call model in layout for use a validation in empty text field for return a error to gust user if any fields are empty. i have a code layout

@using (Html.BeginForm("Sending", "Home"))
{
 <form method="post" id="contactfrm" role="form">
   <div class="col-sm-4">
     <div class="form-group">
       <label for="name">Name</label>
         @Html.TextBoxFor("Name", null, new { id = "name", @class = "form-control", placeholder = "Enter name", title = "Please enter your name (at least 2 characters)" })
@Html.ValidationMessageFor(model => model.Name)
       </div>
       <div class="form-group">
       <label for="email">Email</label>
       @Html.TextBoxFor("Email", null, new { id = "email", @class = "form-control", placeholder = "Enter email", title = "Please enter a valid email address)" })
       @Html.ValidationMessageFor(model => model.Email)
       </div>
    </div>
    <div class="col-sm-4">
<div class="form-group">
<label for="comments">Comments</label>
 @Html.TextArea("Comment", null, new { id = "comments", @class = "form-control", TextMode = "MultiLine", Columns = "3", Rows = "5", placeholder = "Write you message here...", title = "Please enter your message (at least 10 characters)", Style = "resize:none" })
@Html.ValidationMessageFor(model => model.Comment)
</div>
  <button name="submit" type="submit" class="btn btn-lg btn-primary" id="submit">Send Your Message</button>
     <div class="result"></div>
     </div>
     </form>
}

i want to call sending.cs model for use in @Html.ValidationMessageFor(model => model.Name)

How can i call Model in layout?

Thank you

2
  • So what happens when you add @Html.ValidationMessageFor(model => model.Name) to your code? Commented Dec 4, 2015 at 11:52
  • other way around would be adding 'required' HTML attribute Commented Dec 4, 2015 at 12:02

1 Answer 1

1

add your model to your View page like this

@model nameOfModel

and use it like this

 @Html.ValidationMessageFor(model => model.Name) 

and if you want to use only a porperty of it do it like this

@Model.nameOfproperty
Sign up to request clarification or add additional context in comments.

1 Comment

I know it will work with other views but right now i think i have to make a validation based on jQuery which can know me validate after moving take or muse enter event

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.