简体   繁体   English

将字符串模型传递回控制器(MVC表单)

[英]Pass string model back to controller (MVC Form)

I've searched everywhere for an answer to this question, perhaps I am unlucky. 我到处都在寻找这个问题的答案,也许我很倒霉。 This is such a simple thing but I'd rather start here just in case it takes me longer than expected. 这是一件很简单的事情,但是我宁愿从这里开始,以防万一我花了比预期更长的时间。

So I have the simplest view ever. 所以我有史以来最简单的看法。

@model System.String

@using (Html.BeginForm("AdminDeletePost", "Home", FormMethod.Post))
{
    @Html.HiddenFor(m => m)

    <h3>Are you sure you want to delete the following user?</h3>    
    <br />    
    <h4>@Model</h4>    
    <br />    
    <h6>To confirm deletion, click <i>Save</i>.</h6>
    <h6>To cancel, click <i>Cancel</i>.</h6>
}

All I want to do, is pass the value of Model (which is a string) back to the AdminDeletePost action in the Home controller. 我要做的就是将Model的值(是一个字符串)传递回Home控制器中的AdminDeletePost操作。 So it's laid out just like one of my typical forms. 因此它的布局就像我的一种典型形式。 Now that it's simple though, I can't get it to work. 现在,虽然很简单,但我无法使其正常工作。 I should add, that the <h4>@Model</h4> above does display the correct value. 我应该补充一点,上面的<h4>@Model</h4>确实显示了正确的值。 The value of the model is not null. 模型的值不为null。 I'm assuming that the HiddenHelper method is passing metadata of some sort to the InputHelper method, one item of which is the Name parameter. 我假定HiddenHelper方法将某种元数据传递给InputHelper方法,其中一项是Name参数。 However, string isn't something that I can annotate like a normal model. 但是,字符串不是像普通模型那样可以注释的东西。 Perhaps I'm looking at this in the wrong way. 也许我正在以错误的方式看待这个问题。

I can already guess somebody is going to suggest not going the route that I am due to it's simplicity (ie not posting the entire form but rather the individual string). 我已经可以猜到有人会因为简单而建议不要使用我所遵循的路线(即不发布整个表单,而是发布单个字符串)。 However, due to the layout of the application, and maintaining consistency among all of my forms, this is the ideal setup for now. 但是,由于应用程序的布局以及保持所有表单之间的一致性,因此这是目前的理想设置。

Here is the stack... 这是堆栈...

在此处输入图片说明

Has anybody else had any relatively similar issues before? 以前有没有其他人有类似的问题?

These helpers can only take properties of a model; 这些助手只能获取模型的属性。 not the entire model. 不是整个模型。

Otherwise, it would have no way to figure out what name the input should have. 否则,将无法确定输入应使用的名称。

You should either switch to a view model object with one string property (recommended) or write an <input type="hidden"> yourself with appropriate name and value. 您应该切换到具有一个字符串属性的视图模型对象(推荐),或者自己编写具有适当名称和值的<input type="hidden">

Either way, the name must match your action's parameter name for request binding to work. 无论哪种方式,该名称都必须与操作的参数名称匹配,以便请求绑定起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM