ASP.NET MVC2 Form Element Names
I'm trying to make a really simple form in ASP using MVC2 and C#. I have two elements and a submit button, and I made the form using the designer in VS2010.
View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="create_event" runat="server">
<h2>Create</h2>
<p>Event ID:
<asp:TextBox ID="txtEventID" runat="server"></asp:TextBox>
</p>
<p>Event Description:
<asp:TextBox ID="txtEventDescription" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="submit" runat="server" Text="Add Event" />
</p>
</form>
</asp:Content>
The problem I'm having now is when I try to access the form elements in my controller, I get wierd key names using the FormCollection object.
Controller:
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
foreach (var key in collection)
{
var ks = key.ToString();
var value = collection[key.ToString()];
}
开发者_如何学运维 return RedirectToAction("Index");
}
catch
{
return View();
}
}
The name property of the two controls is used for the keys, which gets set at runtime to ctl00$MainContent$txtEventDescription and ctl00$MainContent$txtEventID. Is there any way I can reference these items by the ID's that I set at design time? I tried adding a name attribute with the same value as the id, but that didn't work.
Thanks!
Remove the runat="server"
from the form, that's totally unneeded in MVC. You also shouldn't be using forms collection and should be using model binding. And get rid of the <asp:*>
controls, they're useless in MVC.
Instead, use the Html helpers <%: Html.TextBoxFor(m => model.property) %>
It's much easier to use the supplied Html.BeginForm()
helper, as such:
<%: using (Html.BeginForm()) { %>
.... your stuff
<%: } %>
Upon rereading, i see your problem is that you're using the form designer for a standard asp.net page, not an MVC page.
Try this:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<YourNamespace.YourModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("AddEvent", "YourControllerName", FormMethod.Post, new { id = "create_event" }))
{ %>
<h2>Create</h2>
<p>Event ID: <%= Html.TextBox("txtEventID", Model.txtEventID)%> </p>
<p>Event Description: <%= Html.TextBox("txtEventDescription", Model.txtEventDescription)%> </p>
<button id="submit">Add Event</button>
<% } %>
</asp:Content>
You'll need to create a Model with the properties 'txtEventID' and 'txtEventDescription'... this is the model whose namespace you'll add to the Inherits attribute in the first line (between the angle brackets).
The mistake you're making is using classic ASP controls. MVC deals with pure HTML, and uses HTML helpers to help generate it. HTML.Textbook()
is such a helper.
...matter-of-fact, it's not necessary to use the helpers and model... you can use straight HTML like so:
<p>Event ID: <input type="text" id="txtEventID" name="txtEventID" /> </p>
<p>Event Description: <input type="text" id="txtEventDescription" name="txtEventDescription" /> </p>
My advice: run through the tutorials here: http://www.asp.net/mvc/tutorials.
Lastly, if you're using the Model approach, you can rewrite your Controller actions thus:
public ActionResult Create(YourModel Model)
{
Model.txtEventID ...
}
ASP.NET MVC uses 'model-binding' to bind the values of the form to your Model, so that you don't need to access the FormCollection.
Much to assimilate here, but my advice again is to go through the tutorials to grab the basics. You won't regret it.
what your designer has done is created html mark up as is used in web forms and not as in mvc. What you need is something like "cryotello" wrote
tho you will need to create a entity representation.
What you might be interested in is this following project that has good explanation http://nerddinner.codeplex.com/
Also good investment would be ASP.NET MVC 2 book by Steven Sanderson
it covers loads basic stuff
good luck
精彩评论