English 中文(简体)
如何使用@Html.EditorFor()而不使用观察模型
原标题:How to Use @Html.EditorFor() without the view model

我想做的是这样的事情,这样我就能够制造一种模式的辩证,即I ll在晚期用 j子援引。

<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>

页: 1

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

我发现错误。

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

我不理解它为什么会关注什么地方或什么(只要其正确类型)?

@Html.Partial(“~/Views/joint/EditorTemplates/ViewModel.cshtml”),新的观点是trick的,但我必须宣布模板的全部道路......这只字不提。

这样做有更好的办法?

最佳回答

Technically it s not the instance that s the problem. It s an expression, not a function, that you re passing there and the expression parser used by EditorFor, to get the meta data it uses to identify the properties etc, doesn t support new expressions.

You can simply declare a new instance of the model outside of the EditorFor statement and do this:

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

这应当奏效。

说到——“not,其中使用>model的部分内容,这个表述是一小 we。 您或许应考虑从自己的部分观点中提取方言,这些观点已附有<>意见><>> > /代码>作为模型,然后请您使用<代码>EditorForModel,并在其中使用<>新观点><作为你传递的模式,将其从母体的观点中删除。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签