开发者

Model Binding using ASP.NET MVC, getting datainput to the controller

开发者 https://www.devze.com 2022-12-29 02:58 出处:网络
Pretty Basic one here guys. I have a View which holds 2 textfields for input and a submit button <%using (Html.BeginForm(\"DateRetrival\", \"Home\", FormMethod.Post)){ %>

Pretty Basic one here guys.

I have a View which holds 2 textfields for input and a submit button

<%using (Html.BeginForm("DateRetrival", "Home", FormMethod.Post)){ %>    
<%=Html.TextBox("sday")%>  
<%=Html.TextBox("eday")%>
<input type="submit" value="ok" id="run"/>
<% }%>

the following controller action which I want to bind the data input is as follows

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult DateRetrival()
    {
        return View();
    }


    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult DateRetrival(string submit)
    {


        return null;
    }

When I debug this and look in the action methods parameter, the value i开发者_JAVA百科s null. When I've entered values in both textboxes and and clicked the submit method.


You probably want to do something like this:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult DateRetrival(string sday, string eday)
    {

        return null;
    }

Ideally, though you probably want to be passing a model to your controllers:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult DateRetrival(DateModel dates)
    {
        var date1 = dates.sday;
        var date2 = dates.eday;
        return null;
    }

See http://msdn.microsoft.com/en-us/library/dd394711.aspx


Add parameters to catch each input field value.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DateRetrival(string sday, string eday)
{
    return null;
}


Try:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DateRetrival(string sday, string eday, string submit)
{
    return null;
}

and if you want sumbit button value

<input type="submit" value="ok" id="run" name="submit"/>

If you want to have value posted, name attribute has to be set. Html.TextBox automatically sets name from parameter.

0

精彩评论

暂无评论...
验证码 换一张
取 消