Search code examples
asp.net-mvcmodelactionlink

MVC ActionLink problem


I have a problem with my MVC app. I have a login view

<% Html.EnableClientValidation(); %>

<% using (Html.BeginForm())
   {%>
    <%: Html.ValidationSummary(true)%>

    <fieldset>
        <legend>Login</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Username)%>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Username)%>
            <%: Html.ValidationMessageFor(model => model.Username)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Password)%>
        </div>
        <div class="editor-field">
            <%: Html.PasswordFor(model => model.Password)%>
            <%: Html.ValidationMessageFor(model => model.Password)%>
        </div>

        <p>
            <input type="submit" value="Login" />
        </p>



    </fieldset>
        <%: Html.ActionLink("Set Email",
              "SetEmail",
              "GASLogin",
                          new {model = Model.Username},
              null)%>
<% } %>

and a ActionLink. When i click the actionlink i whant to send an email to the mail address in the text box "<%: Html.TextBoxFor(model => model.Username)%>" . The problem is that my Model.Username is null. So how can i do this?

Thank you.


Solution

  • I think I see the problem. Try redefining the ActionLink so that it doesn't define model = Model.Username, and just let MVC's automatic binding take care of sending the field values to your action method.

    The ActionLink method call gets evaluated when the html is generated from the view, so at that point your model will only contain stuff you've put in it from a previous action method which returns the view. What you want is the value put in the field(s) by the user after the page is displayed and the http post triggered, which is taken care of MVCs binding plumbing.