0

I am fairly new to .NET and I am trying to get mt head wrapped around some simple form and validation syntax stuff.

I have a model ContactUs.cs which looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Presentation.Web.Models.FormModels
{
    public class ContactUs
    {
        public String name { get; set; }
        public String email { get; set; }
        public String comment { get; set; }
    }
}

And my razor looks like this

@using(Html.BeginForm()){
            <div class='form-group'>
                <div class='row'>
                    <div class='col-md-3'>
                        <label class='control-label'>Your Name</label>
                        <input class='form-control' placeholder='your username' type='text'>
                    </div>
                    <div class='col-md-3'>
                        <label class='control-label'>Your Email</label>
                        <input class='form-control' placeholder='your password' type='text'>
                    </div>
                </div>
            </div>
            <div class='form-group'>
                <div class='row'>
                    <div class='col-md-6'>
                        <label class='control-label'>Your Message</label>
                        <textarea class='form-control' name='' rows='4'></textarea>
                    </div>
                </div>
            </div>
        }
<input type="button" ID="sumbmit-contact-us" runat="server" name="Save" value="SUMBMIT"/>

I am struggling to figure out how to dynamically print the label and do validation check on submit. Also if I end up adding this into the HomeController.cs then How should the syntax look like?

1
  • @Dai-- I was actually following this post ryanbutler.org/ASPMVCForm would you not recommend this? Commented Jun 23, 2015 at 22:04

1 Answer 1

1

Use HTML Helpers to render the inputs, labels and validation messages rather than raw markup. Also runat="server" is meaningless in ASP.NET MVC and Razor, as it's a WebForms ASPX feature.

You'll want this:

<div class="col-md-3">
    @Html.LabelFor( m => m.Name );
    @Html.TextBoxForFor( m => m.Name, new { placeholder="Your username" } );
</div>

and so on, for each input

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

Comments

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.