1

I am using MVC3 with HTML views.

Here is my view:

@model mobilesurveys.mt.Models.Question  
@{ ViewBag.Title = "Question"; }
<div data-role="header" data-theme="b">
    <h1>@Model.SurveyName</h1>
</div>

@if (@Model.QuestionType == 7)
{   
    <div data-role="fieldcontain">
    @using (Html.BeginForm("SaveDropDown", "GetQuestion", Model))
    {
        @Html.AntiForgeryToken()
        <fieldset>
            <label class="select">@Model.QuestionText</label><br />
            <select name="selectedObjects" id="selectchoice1" data-native-menu="false">
                <option value="-1">--Select--</option>
                @foreach (var item in Model.Options)
                {
                    if (@item.IsAnswer == true)
                    {
                        <option selected="selected"  value="@item.OptionNumber">@item.OptionText</option>
                    }
                    else
                    {
                        <option  value="@item.OptionNumber">@item.OptionText</option>
                    }
                }
            </select>
        </fieldset>

        <p><input type="submit" id="selectsubmit" value="Next" /></p>
    }
    </div>
} 

@if (@Model.QuestionType == 1)
{
    <div data-role="fieldcontain">
    @using (Html.BeginForm("SaveRadio", "GetQuestion", Model))
    {
        @Html.AntiForgeryToken() 
        <fieldset data-role="controlgroup" id="radio_fieldset">
            <label id="l1" for="select-choice-1" class="select">@Model.QuestionText</label><br />
            @foreach (var item in Model.Options)
            {
                if (@item.IsAnswer == true)
                {
                    <input type="radio" checked="checked" name="selectedObjects" id="@item.OptionText" value="@item.OptionNumber"  onselect="next(@item.OptionNumber);" />
                }
                else
                {
                    <input type="radio" name="selectedObjects" id="@item.OptionText" value="@item.OptionNumber"  onselect="next(@item.OptionNumber);" />
                }
                <label for="@item.OptionText">@item.OptionText</label> 
            }
            <br />
        </fieldset>
        <p><input type="submit" id="radio_submit" onclick="validate(this);" value="Next" /></p>
    }
    </div>
}

Here is my javascript:

<script type="text/javascript">
    $(document).ready(function () {
        // validation for select that can not leave empty.

        $("#selectsubmit").click(function (e) {
            var textContent = $("#selectchoice1").val();
            textContent = jQuery.trim(textContent);
            if (textContent == "-1") {
                alert("please select the value");
                $("#selectchoice1").focus();
                return false;
            }
        });

        $("#radiosubmit").click(function (e) {
            alert('hi');
        });
    });  
</script>

I am redirecting to the same view with a different ID in my controller.

The problem is when the the javaScript is executedonly for the first time the document is loaded. When I click on radiosubmit button the alert is not firing.

How could this be handled in MVC3?

1 Answer 1

2

You bind your event to specific object which have to be already on the page. You have two options here:

  • use jquery live
  • bind to click again after loading second view

Try to google about differences between bind and live (click is just a shortcut to bind on specific event called click)

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

1 Comment

yap thats mainly it, events are not autobound after action has been performed.

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.