1

I have the following view

@using System.Linq;
@model MyCompanyNET.Models.ViewModels.AdministratorViewModel

<style type="text/css">
    span {
        padding-right: 10px;
    }
    ...
</style>

<div class="manage">
    <ul class="nav nav-tabs" id="myTab">
        <li class="active">
            <a data-toggle="tab" href="#invite">
                <span class="glyphicon glyphicon-inbox"></span>Invite
            </a>
        </li>
        ...
    </ul>
    <div class="tab-content">
        <div id="invite" class="tab-pane active fade in">
            @Html.Partial("_InvitePartial", Model)
        </div>
        ...
    </div>
</div>
@section scripts {
    <script>
        $(function () {
            $("input[type=button]").click(function () {
                var data_email = $('#email').val();
                var data_product = $('#product option:selected').text();
                $.ajax({
                    url: '@Url.Action("SendInvite")',
                    type: 'POST',
                    data: { email: data_email, product: data_product },
                    success: function (result) {
                        $('#fail_message').html(result.result_failure);
                        $('#success_message').html(result.result_success);
                    }
                });
            });
        });
    </script>
}

The partial view is

@using (Html.BeginForm("SendInvite", "Tools", FormMethod.Post,
    new
    {
        @class = "form-horizontal",
        enctype = "multipart/form-data",
        role = "form"
    }))
{
    @Html.AntiForgeryToken()
    <h2>Invite Users</h2>
    <div class="form-group">
        @Html.LabelFor(m => m.EmailAddress, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.EmailAddress, new { @class = "form-control", id = "email" })
            @Html.ValidationMessageFor(m => m.EmailAddress)
        </div>
    </div>
    <div class="form-group">
        @Html.Label("Access To", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.DropDownList("Product", new SelectList(
                new List<Object> {
                    new { Text = "Product1", Value = "Product1" },
                    new { Text = "Product2", Value = "Product2" },
                    new { Text = "All", Value = "All" }},
                    "Value",
                    "Text"),
                    new { @class = "form-control", id = "product" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <span id="fail_message" class="label label-danger"></span>
            <span id="success_message" class="label label-success"></span>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="button" value="Invite User" class="btn btn-primary" />
        </div>
    </div>
}

and my controller is

[AllowAnonymous]
public async Task<JsonResult> SendInvite(string email, string product)
{
    ApplicationUser user = null;
    string result = String.Empty;
    if (ModelState.IsValid)
    {
        if (String.IsNullOrEmpty(email))
        {
            return Json(new
            {
                result_success = String.Empty,
                result_failure = "Please enter and email address"
            }, JsonRequestBehavior.AllowGet);
        }
    }

    ... // Do stuff with email and product.
}

Now when this partial view code was in the view itself, everything worked fine. I'd get both the entered email address and the selected product coming through to my controller method SendInvite, but now the button does not even fire.

Now, I have changed the

<input type="button" value="Invite User" class="btn btn-primary"/>

to use submit

<input type="submit" value="Invite User" class="btn btn-primary"/>

This now calls my controller method but without the email parameter, which is always null(?). To make things worse, the return Json(new ... also does not render my <span id="success_message" class="label label-success"></span>s and it used to when everything was in the main view. Why is this occurring and how an I fix it?

Thank for your time.

10
  • Did you look at HTML / debug you JavaScript? Commented Aug 26, 2015 at 20:46
  • Dude, I am so noob I don't know how to debug the javascript. I have tried setting break points in VS2013, in the javascript but they never get hit. Any links or advice? Commented Aug 26, 2015 at 20:58
  • 1
    If you open the debug console in Chrome (not sure if others have it), go to network tab and submit the form. You should see a new request appear in the box, click it and look at the Form Data. I have the sneaking suspicion that it's passing the email under a different name than .NET is expecting. Either way, it will let you know if the problem is your view or your controller. Commented Aug 26, 2015 at 20:59
  • 2
    you need to debug javascript from the browser. Once the view is rendered, MVC is done with it until you call another method. That's the stateless nature of the web. Commented Aug 26, 2015 at 20:59
  • 1
    Guys, thanks so so much for this help, it is most appreciated. I work for a small company and have been asked to develop a new site. It is going well but I have zero experience and this is so valuable to ma, so thanks you very much for your time. Commented Aug 26, 2015 at 21:11

3 Answers 3

2

the name of the email field is EmailAddress not email.. change your parameter name to string EmailAddress or you can change your markup to

@Html.TextBoxFor(m => m.EmailAddress, new { @class = "form-control", Name="email", id = "email" })

trick is the make the name of the field and your parameter name match, not the id.

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

10 Comments

Brilliant explanation man, thanks. Very new to this, so that really helped. One last thing, any idea why my Json return value is now failing to show the labels, instead it just shows a blank screen with text?
This is the answer, a bounty if you can tell me why my labels are failing to render!
sounds like your form is actually submitting instead of being handled by your javascript function.. do you need the <form> at all since you're using ajax to post back the data?
I honestly don't know. I have taken an online example and have built it up. I really have minimal web/MVC knowledge... You think I should take this out?
dotnetfiddle.net/cWvW7x it may not make a ton of sense to you but i tried to point out what html should be in your partial.. and what your javascript should look like.. I am also using a Model for the parameter to your SendInvite action that has an EmailAddress property and a Product property. The javascript should reflect these property names in the post
|
2

Killercam, regarding your form submitting. Did you change it back to type="button" and not type="submit"? If not, reason is when you click the button, it will submit it and will disregard your JavaScript.

Comments

0

try decorating your action method by HttpPost

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.