0

i have a problem with my asp.net mvc3 page. i have a view which has a form in it to create a item of a specific model. it looks likt this:

@model testPro.Models.AddItemModel
@{
    ViewBag.Title = "New";
}
@*<h2>New</h2>*@

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>New Item</legend>

                <div class="editor-label">
                    @Html.LabelFor(model => model.UserItem.name)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.UserItem.name)
                    @Html.ValidationMessageFor(model => model.UserItem.name)
                </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script>
    $(document).ready(function () {

        alert("test");
        $("#UserItem_Name").html("bla");
        $("#UserItem_Name").attr('disabled');
    });
</script>

at the end i have my jquery stuff which should change the text-box.

the problem is, nothing is changing and i dont know why

and yes: i have jquery included in the header.

3
  • Is this a view or a partial view that you include with an AJAX request? Commented Jul 5, 2012 at 9:19
  • are you sure you are also referencing the core jquery library? Commented Jul 5, 2012 at 9:20
  • You are referring to edit box by ID ("UserItem_Name"), but please check first that your input element has such ID. If not, add it explicitly - it should be something like @Html.EditorFor(model => model.UserItem.name, new { id = "UserItem_Name" }) Commented Jul 5, 2012 at 9:26

1 Answer 1

1

Try:

<script type="text/javascript">
    $("#UserItem_name").val('bla');
    $("#UserItem_name").attr('disabled', 'disabled');
</script>

Things to notice:

  • Your textbox id is UserItem_name, not UserItem_Name which obviously is not the same thing
  • Use .val() to set values of form input fields instead of .html()
  • The .attr() method takes 2 arguments when you want to set an attribute value: 1) the attribute name and 2) the attribute value. You are currently passing only one parameter, so you are basically reading the attribute value, not setting it.

Also if this code is inside a partial view that you have injected into your DOM after an AJAX request, you should remove the $(document).ready handler.

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

2 Comments

no, this is fine, its the same name like in the html which comes out later
Are you sure? Because @Html.EditorFor(model => model.UserItem.name) will generate: <input class="text-box single-line" id="UserItem_name" name="UserItem.name" type="text" value="" />. Are you using a custom editor template to change the id or something? Because by default this will most certainly generate id="UserItem_name".

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.