How to use @ Html.EditorFor () without a view model

I want to do something like this, so I can create a modal dialog that I will call later using jQuery

<div class="modal" id="modalName" style="display: none;">
<div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3>Edit Contacts</h3>
</div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new Dictionary<string, object> { { "class", "form-horizontal" } }))
{
    <div class="modal-body">
    @Html.EditorFor(model => new ViewModel(), "ViewModelTemplateName")
    </div>
    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">Close</a>
        <button type="submit" class="btn btn-primary">
            Submit</button>

    </div>
}
</div>

In this line

@Html.EditorFor(model => new ViewModel(), "ViewModelTemplateName")

I get an error

Templates can only be used with field access, property access, a one-dimensional array index, or a one-parameter custom expression index.

I don’t understand why he would be interested in where and what is an instance (to its correct type)

@ Html.Partial ("~ / Views / Shared / EditorTemplates / ViewModel.cshtml", the new ViewModel ()) does the trick, but I have to declare the full path of the template ... it sucks.

so is there a better way to do this?

+5
source share
1 answer

, . , , , , EditorFor, , .., new.

EditorFor :

@{ var emptyViewModel = new ViewModel(); }
@Html.EditorFor(model => emptyViewModel, "ViewModelTemplateName") 

.

- model . , , , ViewModel , EditorForModel new ViewModel() , .

+12

All Articles