Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.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# 如何使用POST-ASP.NETMVC将复杂对象从视图传递到控制器_C#_Asp.net_Asp.net Mvc_View_Parameter Passing - Fatal编程技术网

C# 如何使用POST-ASP.NETMVC将复杂对象从视图传递到控制器

C# 如何使用POST-ASP.NETMVC将复杂对象从视图传递到控制器,c#,asp.net,asp.net-mvc,view,parameter-passing,C#,Asp.net,Asp.net Mvc,View,Parameter Passing,嗨,我试图找到一个解决我的问题的办法,但我做不到。我有一堂课,有一篇论文和许多关键词 public class ThesisWithKeywordsModel { public Thesis thesis { get; set; } public IQueryable<Keyword> keywords { get; set; } public IEnumerable<int> checkboxList { get;

嗨,我试图找到一个解决我的问题的办法,但我做不到。我有一堂课,有一篇论文和许多关键词

public class ThesisWithKeywordsModel
    {
        public Thesis thesis { get; set; }
        public IQueryable<Keyword> keywords { get; set; }
        public IEnumerable<int> checkboxList { get; set; }
    }

问题是对象带有关键字的ISIS模型主题为空,主题.主题,主题.复选框列表均为空。有趣的是,当我在响应体中看到响应时,我有POST参数,如果我用关键字修改siswithkeywordsModel thesisthesis thesis thesis thesis thesis thesis thesis thesis thesis thesis thesis thesis thesis thesis thesis thesis desis我有从表单中发布的所有值。

首先,我不会尝试对
关键字
属性使用IList。其次,最好将关键字和复选框列表组合到一个属性中,使关键字类如下所示:

public class Keyword
{
    public int KeywordId {get;set;}
    public bool Checked {get;set;}
}
public class ThesisWithKeywordsModel
{
    public Thesis thesis { get; set; }
    public IList<Keyword> keywords { get; set; }
}
因此,
siswithkeywords模型将如下所示:

public class Keyword
{
    public int KeywordId {get;set;}
    public bool Checked {get;set;}
}
public class ThesisWithKeywordsModel
{
    public Thesis thesis { get; set; }
    public IList<Keyword> keywords { get; set; }
}
接下来,在向视图传递值列表或从视图传递值列表时,需要将cshtml中的
foreach
循环更改为
for
循环

for(var i = 0; i < Model.keywords.Count; i++)
{
    <input type="checkbox" id="@Model.keywords[i].KeywordId" name="checkboxList" checked='@Model.keywords[i].Checked ? "checked" : ""'>
    <label for="@Model.keywords[i].KeywordId">@Model.keywords[i].Value</label><br/>
}
for(var i=0;i
}

尝试将参数名称“thesis”更改为其他名称。我不认为它来自名称,因为如果我使用thesis-thesis,我会得到thesis的所有信息。从安蒂昂到控制器一切正常,但之后。。。也许我必须以另一种方式返回结果,但我不知道如何返回。这是我在请求论文中传递的内容。列表:test“论文”。描述:test checkboxList:1 checkboxList:2“但在操作中-所有内容都为空,这会使您混淆
ModelBinder
,因为操作方法中的参数名称与模型中的属性名称相同。将该方法更改为
public ActionResult createdeposition(thesiswithkeywords模型)
,它应该绑定correctly@StephenMuecke,这是我的建议,但Trifon Dardzhonov似乎没有尝试。哇,这很有帮助:)我是asp mvc新手,我花了一整天的时间来解决这个愚蠢的问题:D非常感谢:)
thesis.keywords = db.KeywordRepository.GetAllKeywords()
                     .Select(kw => new Keyword
                     {
                        KeyeordId = kw.KeywordId,
                        Checked = false
                     }).ToList();
for(var i = 0; i < Model.keywords.Count; i++)
{
    <input type="checkbox" id="@Model.keywords[i].KeywordId" name="checkboxList" checked='@Model.keywords[i].Checked ? "checked" : ""'>
    <label for="@Model.keywords[i].KeywordId">@Model.keywords[i].Value</label><br/>
}