razor editorfor checkbox event binding

早过忘川 提交于 2019-12-22 09:14:59

问题


I cant bind event to @html.editorfor

I've heard it might be connected to presence of "class" attribute but I can see no connection whatsoever.

@Html.EditorFor(model => model.SameAsBaseAddress, new { @onclick = "checkboxcheck()" })

function checkboxcheck() {
    //blah...
}

When I debug with firebug and add handler manually all works fine. TextBoxFor, RadioButtonFor etc. have no problems with razor event binding.

What to do?


回答1:


The EditorFor extension doesn't have an argument htmlAttributes like the TextBoxFor, see: http://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editorfor%28v=vs.118%29.aspx

I suggest you change EditorFor to CheckBoxFor, or if you do want to use the EditorFor, this question suggests to create an editor template.




回答2:


Modify your EditorFor to use the following instead. I've used this to add html attributes (including classes and onclick) to an EditorFor which generates a checkbox.

@Html.EditorFor(model => model.SameAsBaseAddress, new { htmlAttributes = new { @onclick = "checkboxcheck()" } })

You can add more attributes as a comma separated list:

@Html.EditorFor(model => model.SameAsBaseAddress, new { htmlAttributes = new { @onclick = "checkboxcheck()", @class = "checkbox" } })


来源:https://stackoverflow.com/questions/20700734/razor-editorfor-checkbox-event-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!