0

I have the following code in my NewMember.cshtml View file:

@{
    ViewBag.Title = "New Member";
}

<div id="newForm">
    <table>
        <tr>
            <td class="first"><label title="Date" runat="server">Date</label></td>
            <td class="second"><input id="DateBox" value="test"" /></td>
        </tr>
        <tr>
            <td class="first"><label title="FirstName" runat="server">First Name</label></td>
            <td class="second"><input id="FirstNameBox" runat="server" /></td>
        </tr>
        <tr>
            <td class="first"><label title="LastName" runat="server">Last Name</label></td>
            <td class="second"><input id="LastNameBox" runat="server" /></td>
        </tr>
        <tr>
            <td class="first"><label title="DOB" runat="server">Date of Birth</label></td>
            <td class="second"><input id="DOBBox" runat="server" /></td>
        </tr>
    </table>
    <input type="submit" value="Submit"/>
</div>

And my Controller file for this contains the following method:

public ActionResult NewMember()
{
    return View();
}

I am using Microsoft's SQL Server 2008 to hold these information. I know how to make a query to the database using c#, but I don't understand how to get the inputted data from the View file to the Controller.

Any help would be greatly appreciated.

Thanks

EDIT: a little bit more information - I've been looking around the internet for this problem and I seen some uses Javascript. Unfortunately, I do not know any javascript, so is there a way to do this without using javascript?

6
  • 3
    runat=server is for WebForms, not MVC. Let's take a look to any MVC example. Commented Nov 12, 2012 at 16:57
  • 1
    you should be using the helpers included in MVC msdn.microsoft.com/en-us/library/…. else you will have to use javascript and give your fields an id Commented Nov 12, 2012 at 16:57
  • @Adriano, Many thanks for the link. However, I have looked at the website before, but most of the examples uses aspx files while I uses cshtml files (Razor). Is there any other examples or link you can send? Thanks Commented Nov 12, 2012 at 17:06
  • @Danny actually that example is with Razor... Commented Nov 12, 2012 at 17:12
  • @Danny You need BeginForm method, look tutorial from Adriano comment: asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/… Commented Nov 12, 2012 at 17:13

1 Answer 1

1

Unless you want ajax calls, you don't have to use javascript

Give input elements a name in your form and lose the runat="server"

View:

<form id="newForm" method="POST" action="@Url.Action("NewMember", "CONTROLLER NAME")">
<table>
    <tr>
        <td class="first"><label title="Date">Date</label></td>
        <td class="second"><input id="DateBox" value="test"" name="date" /></td>
    </tr>
    <tr>
        <td class="first"><label title="FirstName">First Name</label></td>
        <td class="second"><input id="FirstNameBox" name="fname" /></td>
    </tr>
    <tr>
        <td class="first"><label title="LastName">Last Name</label></td>
        <td class="second"><input id="LastNameBox" name="lname" /></td>
    </tr>
    <tr>
        <td class="first"><label title="DOB" >Date of Birth</label></td>
        <td class="second"><input id="DOBBox" name="birth" /></td>
    </tr>
</table>
<input type="submit" value="Submit"/>
</form>

Then in your controller create an action to handle posts:

Controller:

[HttpPost]
public ActionResult NewMember(string date, string fname, string lname, string birth)
{
   return View();
}

Hope this helps!

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.