Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 执行HTTPPOST MVC2_C#_Asp.net Mvc 2_Http Post - Fatal编程技术网

C# 执行HTTPPOST MVC2

C# 执行HTTPPOST MVC2,c#,asp.net-mvc-2,http-post,C#,Asp.net Mvc 2,Http Post,我有一个MVC2应用程序。在此应用程序中,我有一个与modell NewHorseModel竞争的strongtyped视图: public class NewHorseModel { public List<Category> Faehigkeit { get; set; } } public class Choice { public int Id { get; set; } public string Name { get; set; } pu

我有一个MVC2应用程序。在此应用程序中,我有一个与modell NewHorseModel竞争的strongtyped视图:

public class NewHorseModel
{
    public List<Category> Faehigkeit { get; set; }
}

public class Choice
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Beschreibung { get; set; }
    public bool Selected { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Beschreibung { get; set; }
    public List<Category> Subcategories { get; set; }
    public List<Choice> Choices { get; set; }
    public int Parent { get; set; }
}
NewHorseModel中的Faehigkeit为空

这里是我的ASPX代码:

<div id="Div2">
    <%
    foreach (Category item2 in Model.Faehigkeit)
    {
        Html.RenderPartial("Faehigkeit", item2);
    }
    %>
</div>
如果我尝试获取Faehigkeit->Choices的值,那么Faehigkeit的名称、Faehigkeit的ID都是空的,并且没有选择 显示调试期间NewHorseModel内容的图像:


多谢各位

通常,如果您将HttpPost作为操作的属性,它类似于:

[HttpPost]
public void myAction(NewHorseModel newHorseModel)
{;}

然后把类类型作为你的参数,它应该为你自动绑定,如果你需要做一些疯狂的事情,或者从外部非ASP页面发布此操作,您可以使用FormCollection作为您的论据,这是一本包含所有元素和值的美化词典。

我写了两篇博客文章,将解决您的问题:

本文将解释如何将IList发布到控制器操作,并将从头到尾指导您,以便您了解它实际上是如何工作的以及为什么

而且,由于客户端可能有复杂的JSON对象,并且希望它们像您的案例一样在操作方法中绑定到服务器上,因此这可能是一个有趣的阅读。它解释了发送复杂JSON对象的问题,并提供了一个简单的jQuery插件,该插件将客户机对象转换为一个表单,该表单可以轻松地将数据绑定到强类型操作方法参数

注意:正如Phil Haack所解释的,也有可能使用JsonValueProviderFactory,但该解决方案需要在客户端和服务器端进行更改,因为您使用的是MVC2

基于您的代码 您只提供了代码的一些部分,还有一些不是真正相关的,但无论如何,我将对您的案例进行观察

要在服务器上建模绑定的所有输入字段都需要有正确的名称。从呈现的复选框名称中,我们可以看到情况根本不是这样

根据您的模型,您的输入应命名为“无所谓输入类型”,因为只有您知道哪些输入应被呈现,以及呈现为哪种类型:

<!-- erste fähigkeit -->
<input name="Faehigkeit[0].Id" />
<input name="Faehigkeit[0].Name" />
<input name="Faehigkeit[0].Beschreibung" />
<input name="Faehigkeit[0].Subcategories[0].Id" />
<input name="Faehigkeit[0].Subcategories[0].Name" />
...
<input name="Faehigkeit[0].Choices[0].Id" />
<input name="Faehigkeit[0].Choices[0].Name" />
<input name="Faehigkeit[0].Choices[0].Beschreibung" />
<input name="Faehigkeit[0].Choices[0].Selected" />
...
<input name="Faehigkeit[0].Choices[x].Id" /> <!-- "x" could be any number -->
...
<input name="Faehigkeit[0].Parent" />
<!-- zwite fähigkeit -->
<input name="Faehigkeit[1].Id" />
...
<!-- usw. -->
如果您的客户端视图直接操作了该对象,那么您可以随后通过jQuery Ajax调用ie将其发送回服务器:

$.ajax({
    type: "POST",
    url: "someController/myAction",
    data: $.toDictionary(data), // this is my jQuery plugin mentioned earlier
    success: function() { ... },
    error: function() { ... }
});
因此,您可以选择,但无论哪种情况,字段名称都应该正确,否则您的模型将不会绑定到参数,您将无法使用它

您可以尝试使用m;%%>但这可能不是你想使用的形式。在这种情况下,必须使用手动输入命名。或者您也可以编写一些自定义代码

其中一种方法是将局部视图模式类型更改为例如Tuple。字符串将告诉它应该在字段名中预加哪个字符串

Html.RenderPartial("Faehigkeit", Tuple.New("Faehigkeit[0]", item2));
然后,当您继续操作时,应始终添加以前的数据,以便子类别的列表及其受尊重的子对象具有正确的输入表单名称


不太好,但我想这是唯一的办法。

你的HTML看起来一团糟。。。您应该发布服务器端视图代码ASPX,而不是从浏览器窗口复制的客户端HTML呈现。请提供您的ASPX视图代码。顺便说一句:在代码中混合使用德语和英语不是很奇怪吗?看起来很乱。最好留一个。既然框架是英语的,那么它应该是英语的。我也不是英国人,我的代码是英语。只是一个观察。@user655293:我已经对我的答案进行了相当大的修改。看看它是否有用。@Robert Koritnik->Mixed languages:你是对的,它有点混乱,我必须清理代码。使用FormCollection是非常糟糕的做法,因为你失去了所有模型类验证,必须手动执行。我不建议这样做。Asp.net MVC最好的部分之一是它的自动服务器端验证。非常感谢,这似乎是正确的方法。我已经更新了我的代码,但它不工作@用户655293:检查我答案中极其重要的部分。非常感谢,最后一个问题:代码:工作正常。但是所选的值为ervery time false,但它应该等于复选框的状态已选中或未选中->true和false,您有办法解决此问题吗?@user655293:有。复选框是Asp.net MVC中的一个例外。如果您检查HtmlHelper扩展方法如何呈现它们,您会看到它是通过两个字段来实现的:值设置为true的visible CheckBox和值设置为false的同名隐藏输入。因此,对于每个复选框,您必须添加两个字段,并且不要忘记为它们设置值。这两个都是。我遵循了您的手册:在httppost之后得到的值每次都是false。如果我选中一些复选框,它不会变为真。有什么想法吗?我真的非常感谢你的帮助!!!
     <input type="hidden"  name="Faehigkeit[<%=Model.Id%>].Id" value="<%=Model.Id%>" />
<input type="hidden" name="Faehigkeit[<%=Model.Id%>].Name" value="<%: Model.Name%>" />
<input type="checkbox" name="Faehigkeit[0].Choices[<%=Model.Id%>].Selected" />
  <input type="hidden"  name="Faehigkeit[1].Id" value="1" />
<input type="hidden" name="Faehigkeit[1].Name" value="Qualit&#228;t der Gangarten" />
<input type="hidden" name="Faehigkeit[1].Choices[4].Id" value="4" />
<input type="checkbox" name="Faehigkeit[1].Choices[4].Selected" />
public ActionResult CreateNewHorse(NewHorseModel collection)
    {
        if (User.Identity.IsAuthenticated)
        {


            return View();
        }
        else
        {
            return View("Account/LogOn");
        }

    }
[HttpPost]
public void myAction(NewHorseModel newHorseModel)
{;}
<!-- erste fähigkeit -->
<input name="Faehigkeit[0].Id" />
<input name="Faehigkeit[0].Name" />
<input name="Faehigkeit[0].Beschreibung" />
<input name="Faehigkeit[0].Subcategories[0].Id" />
<input name="Faehigkeit[0].Subcategories[0].Name" />
...
<input name="Faehigkeit[0].Choices[0].Id" />
<input name="Faehigkeit[0].Choices[0].Name" />
<input name="Faehigkeit[0].Choices[0].Beschreibung" />
<input name="Faehigkeit[0].Choices[0].Selected" />
...
<input name="Faehigkeit[0].Choices[x].Id" /> <!-- "x" could be any number -->
...
<input name="Faehigkeit[0].Parent" />
<!-- zwite fähigkeit -->
<input name="Faehigkeit[1].Id" />
...
<!-- usw. -->
var data = {
    Faehigkeit: [
        {
            Id: 1,
            Name: "Some name",
            Beschreibung: "Some description",
            Subcategories: [
                { ... },
                { ... },
                ...
            ],
            Choices: [
                { ... },
                { ... },
                ...
            ]
        },
        { ... },
        ...
    ]
};
$.ajax({
    type: "POST",
    url: "someController/myAction",
    data: $.toDictionary(data), // this is my jQuery plugin mentioned earlier
    success: function() { ... },
    error: function() { ... }
});
Html.RenderPartial("Faehigkeit", Tuple.New("Faehigkeit[0]", item2));