How to use Knockout’s data-bind attribute in Mvc helpers like Html.EditorFor()

How to use Knockout’s data-bind attribute in Mvc helpers like Html.EditorFor()

Asked on December 21, 2018 in knockout js.
Add Comment


  • 2 Answer(s)

        The 3rd parameter to EditorFor is not doing the expected work :

        Look at this link : http://msdn.microsoft.com/en-us/library/ff406461(v=vs.98).aspx

        EditorFor is not including the  HTML attributes to the element. Rather than this Use the  TextBoxFor

    @Html.TextBoxFor(model => model.Name, new { data_bind = "value:firstName" });
    
    

    Answered on December 21, 2018.
    Add Comment

    This solution is helpful :

        Transfer  the name of the field  to the editor template as part of the additional ViewData like this :

    @Html.EditorFor(t => t.name, "", new { fieldName = "name" })
    

        Then in the editorTemplate , Do this :

    @{
        var fieldName = ViewData.Where(v => v.Key == "fieldName").FirstOrDefault().Value;
    }
    @Html.TextBox("", Model, new { data_bind = "value:"+fieldName })
    
    Answered on December 21, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.