English 中文(简体)
ASP.NET MVC的吞 是否提交了价值?
原标题:Does ASP.NET MVC swallow submit values?

我在伙伴关系中有一个单一形式。 NET MVC(v1)有2个投入县。 每一提交纽芬兰语都必须以这种单一形式收载,我需要知道哪一个用户要求使用。

我知道,要检查按纽芬兰文将归还的表格集市值。 例如,如果我和用户点击Button2,我就应该能够提出请求。 表格[“Button2”]! 如果情况是我知道用户点击了该纽特,那将作这样的评价。

然而,这不利于我。 我所有县的价值观都是无效的,因为它们不包含在请求中。 形式价值。 伙伴关系中有点。 NET MVC 那些吞s这些价值观?

下面是我的形式守则:

<% using (Html.BeginForm()) {%>

    <% Html.RenderPartial( "EditAreaControl", Model ); %>

    <div class="form-layout-command-container">
        <div class="form-layout-command-area-alpha"><button type="submit" name="submit1" value="Save">Save</button></div>
        <div class="form-layout-command-area-alpha"><button type="submit" name="submit2" value="SaveAndCopy">Save and Create Copy</button></div>
        <div class="form-layout-command-area-beta"><%= Html.ActionLink("Cancel", "list") %></div>
    </div>

<% } %>

这里是我的控制员守则:

[AcceptVerbs( HttpVerbs.Post )]
public ActionResult Add(FormCollection values )
{
   if (values["submit1"] != null)
        // always false
   if (values["submit2"] != null)
        // always false as well
}
最佳回答

我在使用火药时发现,提交纽州没有在答复中发出,因此,我无法在多国军方面做很多事。 我决定使用一个客户旁边的 ha车,在客户方面填充一个隐蔽的输入场,将传给控制器的数值。

我将投入纽扣地改为:

<input type="submit" value="Save" onclick="actions.copyValues($(this), $( #submitAction ));" />
<input type="submit" value="Save and Copy" onclick="actions.copyValues($(this), $( #submitAction ));" />
<input type="hidden" id="submitAction" name="submitAction" />

The jquery 则简单地复制了价值观:

Actions.prototype.copyValues = function(from, to) {
            $(to).val($(from).val());
        };

管制者的行动则着眼于隐藏的投入价值:

var request = HttpContext.Request;
return request.Form["submitAction"];

这从上述问题解决了,但我认识到,这个问题不是干净的。

问题回答

http://www.w3schools.com/TAGS/att_button_ Value.asp”rel=“nofollow noreferer”>w3schools:

重要: 如果你以超文本形式使用顿元件,不同的浏览器将提交不同的数值。 互联网探索者将提交标的,而其他浏览器将提交价值归属的内容。 利用投入元素以超文本形式创建纽吨。

这似乎并不标准化。 你应该坚持下去。

<input type="submit" name="submitButton" value="Save" />
<input type="submit" name="submitButton" value="Cancel" />

我将使用提交的类型投入,而不是按纽扣。 非投入不得以职位的形式收回,或至少可以不连贯地收回。 请注意,它们的名称与不同的数值相同,以便你能够将相同的参数用于提交表格的任何县。

<input type="submit" name="submitButton" value="Save" />
<input type="submit" name="submitButton" value="SaveAndCopy" />


public ActionResult Save( string submitButton, ... )
{
     if (submitButton == "Save")
     {
        ...
     }
     else if (submitButton == "SaveAndCopy")
     {
        ...
     }

     ....
}

以两种不同形式提出,你将知道根据哪一种方式要求控制员采取行动。





相关问题
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. ...