jqueryasp.net-mvcmodel-view-controllereditorfor

How can I get value of editorfor and assign this value to another editorfor?


I want to get value of my projectName but Alert result always is :

Value: function(value){
return access(this, function(value){ 
return value === undefined ? 
Jquery.text(this):
this.empty().each(function(){…. 

like that. I tried .val and .html methods. But they don't work. I also changed the EditorFor as a TextBoxFor but again doesn't work. What should I do? Thanks in advance.

here my view:

<script>
$(document).ready(function () {
    $("#projectName").change(function(){
        alert("Value: " + $("#identifier").text);
    });
});
</script>

<div class="form-group">
    @Html.LabelFor(model => model.projectName, new { @class = "col-lg-2 control-label" })

    <div class="col-lg-10">
        <p>@Html.EditorFor(model => model.projectName, new { htmlAttributes = new { @class = "form-control", id = "projectName" } })</p>
        @Html.ValidationMessageFor(model => model.projectName)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.identifier, htmlAttributes: new { @class = "col-lg-2 control-label" })
    <div class="col-lg-10">
        <p>@Html.EditorFor(model => model.identifier, new { htmlAttributes = new { @class = "form-control", id = "identifier" } })</p>
        @Html.ValidationMessageFor(model => model.identifier)
    </div>
</div>

Solution

  • Try :-

    $(document).ready(function () {
       $("#projectName").on('input',function(){
          $('#identifier').val($(this).val());
       });
    });