0

I am learning ASP MVC and I try do form, and when you click the button on the form, I want to add some content to div in this form.

@using (Html.BeginForm())
{
    <div>
     <input type="button" value="Click to edit address" onclick="@String.Format("addAddress()")" />
    <div id='divResult'>

        Here I want to add new conntent after click button.

    </div>
    <div>
}

My Java Script function does't work:

<script type="text/javascript">
    function addAddress() {
            $('#divResult').replaceWith("@Html.EditorFor(m => m.User.HomeAddress)");
        }
</script>

Can anyone help me? I want to fill my User (model.User) object and post filled to server, but Athe address you can fill only after click button, because it has a lot of field to be filled, but address is not necessary.

2
  • Why just add it, hide (with css), and show when user presses the button? Theoreticaly, your code should work any error? You should try also wrap it with Html.Raw: @Html.Raw(Html.EditorFor(...)). Commented Aug 11, 2014 at 11:23
  • Is your script placed in cshtml? Commented Aug 11, 2014 at 12:26

1 Answer 1

1

Try this:

@using (Html.BeginForm())
{
<div>
    <input type="button" value="Click to edit address" onclick="addAddress()" />
    <div id='divResult' hidden='hidden' >
        @Html.EditorFor(m => m.User.HomeAddress)
    </div>
</div>
}

-

<script type="text/javascript">
    function addAddress() {
        $('#divResult').show();
    }
</script>
Sign up to request clarification or add additional context in comments.

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.