ASP.NET MVC 3 Model没有关于回发的信息

时间:2012-02-22 18:46:07

标签: asp.net-mvc-3

我有一个视图,其中有几个部分视图,我绑定到我的模型。出于某种原因,当我发布时,模型是空的,我不知道为什么。

以下是我的ViewModel。

public class IndexViewModel
{
    public bool AdvancedSearchOption { get; set; }
    public bool ForceAdvanced { get; set; }
    public bool ForceSimple { get; set; }
    public string SimpleSearchCriteria { get; set; }
    public string CustomerNumberCriteria { get; set; }
    public string AccountNumberCriteria { get; set; }
    public string NameCriteria { get; set; }
    public string PhoneNumberCriteria { get; set; }
}

这是我的控制器。我正在填写视图模型的所有值,因为我想查看值是否到达部分视图。他们确实到了那里,所以只是在帖子上我有问题。

public class HomeController : Controller
{
    private ISecurityRepository SecurityRep;

    public HomeController(ISecurityRepository repo)
    {
        SecurityRep = repo;
    }

    public ActionResult Index()
    {
        IndexViewModel temp = new IndexViewModel();
        temp.AdvancedSearchOption = SecurityRep.DefaultToAdvancedSearch(User.Identity.Name);
        temp.ForceAdvanced = false;
        temp.ForceSimple = false;
        temp.SimpleSearchCriteria = "Testing";
        temp.AccountNumberCriteria = "Acct";
        temp.CustomerNumberCriteria = "Cust";
        temp.NameCriteria = "Name";
        temp.PhoneNumberCriteria = "Phone";
        return View(temp);
    }

    public ActionResult SimpleSearch()
    {
        IndexViewModel temp = new IndexViewModel();
        temp.AdvancedSearchOption = SecurityRep.DefaultToAdvancedSearch(User.Identity.Name);
        temp.ForceAdvanced = false;
        temp.ForceSimple = true;
        temp.SimpleSearchCriteria = "Testing";
        temp.AccountNumberCriteria = "Acct";
        temp.CustomerNumberCriteria = "Cust";
        temp.NameCriteria = "Name";
        temp.PhoneNumberCriteria = "Phone";
        return View("Index",temp);
    }

    public ActionResult AdvancedSearch()
    {
        IndexViewModel temp = new IndexViewModel();
        temp.AdvancedSearchOption = SecurityRep.DefaultToAdvancedSearch(User.Identity.Name);
        temp.ForceAdvanced = true;
        temp.ForceSimple = false;
        temp.SimpleSearchCriteria = "Testing";
        temp.AccountNumberCriteria= "Acct";
        temp.CustomerNumberCriteria= "Cust";
        temp.NameCriteria= "Name";
        temp.PhoneNumberCriteria = "Phone";
        return View("Index", temp);
    }

    [HttpPost]
    public ActionResult Index(IndexViewModel vm, FormCollection formCollection)
    {
        return View();
    }
}

这是我的观点

@model TRIOSoftware.Magnum.Models.IndexViewModel

@{
    ViewBag.Title = "Search";
}

@if ((@Model.AdvancedSearchOption && @Model.ForceSimple != true) || @Model.ForceAdvanced == true)
{
    @Html.Partial("AdvancedSearch")
}
else
{
    @Html.Partial("SimpleSearch")
}

这是我的SimpleSearch部分视图。我想如果我能让这个工作,另一个将遵循相同的道路。我在局部发帖,我用jQuery来做。我不确定这些事情中的任何一个是否会引起我的问​​题。我只有所有隐藏的物品,因为我不知道是否没有它们导致我的问题。

 @model TRIOSoftware.Magnum.Models.IndexViewModel

 <script type="text/javascript">
     $(document).ready(function () {
         $("#DefaultDiv").find("#DefaultAdvanced").click(function () {
             $.post("DefaultSimple");
         });

         $("#SearchSection").find("#SearchButton").click(function () {
             $.post("");
         });
     });
</script>

 @using (Html.BeginForm("Index","Home"))
 {
     @Html.HiddenFor(m => m.ForceAdvanced)
     @Html.HiddenFor(m => m.AdvancedSearchOption)
     @Html.HiddenFor(m => m.ForceSimple)
     @Html.HiddenFor(m => m.AccountNumberCriteria)
     @Html.HiddenFor(m => m.CustomerNumberCriteria)
     @Html.HiddenFor(m => m.NameCriteria)
     @Html.HiddenFor(m => m.PhoneNumberCriteria)

     <div id="DefaultDiv" style="float:right">
         <a id="DefaultAdvanced" href="#" class="ButtonClass">Default Simple Search</a>
     </div>

     <div style="clear:both; margin: auto; width: 800px">
         <img src="../../Content/images/TRIO_transparent_image.gif"; alt="TRIO Software"; style="margin-left:150px; clear:left"/>
             <div style="clear:left; float: left" class="SearchText">
                  @Html.Label("What's your inquiry?:")
                  @Html.EditorFor(m => m.SimpleSearchCriteria, new { style = "width: 400px" })
             </div>
             <div id="SearchSection" style="float: left" class="SearchText">
                 <a href="#"; id="SearchButton" class="ButtonClass"><img src="../../Content/images/Search.gif"; alt="Search"; style="float:left" /></a>
             </div>
             <p style="clear:left;margin-left:400px">
                 @Html.ActionLink("Advanced Search", "AdvancedSearch", null, new { style = "clear:left" })
             </p>

    </div>
 }

以下是查看简单搜索部分视图时的HTML代码:

<div id="main">
    <script type="text/javascript">
        $(document).ready(function () {
            $("#DefaultDiv").find("#DefaultAdvanced").click(function () {
                $.post("DefaultSimple");
            });

            $("#SearchSection").find("#SearchButton").click(function () {
                $.post("");
            });
        });
    </script>

    <form method="post" action="/">
        <input type="hidden" value="False" name="ForceAdvanced" id="ForceAdvanced" data-val-required="The ForceAdvanced field is required." data-val="true">
        <input type="hidden" value="False" name="AdvancedSearchOption" id="AdvancedSearchOption" data-val-required="The AdvancedSearchOption field is required." data-val="true">
        <input type="hidden" value="False" name="ForceSimple" id="ForceSimple" data-val-required="The ForceSimple field is required." data-val="true">
        <input type="hidden" value="Acct" name="AccountNumberCriteria" id="AccountNumberCriteria">
        <input type="hidden" value="Cust" name="CustomerNumberCriteria" id="CustomerNumberCriteria">
        <input type="hidden" value="Name" name="NameCriteria" id="NameCriteria">
        <input type="hidden" value="Phone" name="PhoneNumberCriteria" id="PhoneNumberCriteria">
        <div style="float:right" id="DefaultDiv">
            <a class="ButtonClass ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" href="#" id="DefaultAdvanced" role="button"><span class="ui-button-text">Default Simple Search</span></a>
        </div>
        <div style="clear:both; margin: auto; width: 800px">
            <img style="margin-left:150px; clear:left" alt="TRIO Software" ;="" src="../../Content/images/TRIO_transparent_image.gif">
            <div class="SearchText" style="clear:left; float: left">
               <label for="What_s_your_inquiry_:">What's your inquiry?:</label>
               <input type="text" value="Testing" name="SimpleSearchCriteria" id="SimpleSearchCriteria" class="text-box single-line">
            </div>
            <div class="SearchText" style="float: left" id="SearchSection">
                <a class="ButtonClass ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="SearchButton" ;="" href="#" role="button"><span class="ui-button-text"><img style="float:left" alt="Search" ;="" src="../../Content/images/Search.gif"></span></a>
            </div>
            <p style="clear:left;margin-left:400px">
                <a style="clear:left" href="/Home/AdvancedSearch">Advanced Search</a>
            </p>
     </div>
    </form>
</div>

如何解决此问题?

3 个答案:

答案 0 :(得分:1)

虽然您选择了部分渲染,但您没有将模型传递给它。有一个Html.Partial的重载版本,它带有第二个参数,允许你将模型传递给它:

@Html.Partial("ViewName", Model);

所以在你的情况下,你会使用它:

@if ((Model.AdvancedSearchOption && Model.ForceSimple != true) || Model.ForceAdvanced == true) 
{ 
    @Html.Partial("AdvancedSearch", Model)  
} 
else 
{ 
    @Html.Partial("SimpleSearch", Model)  
} 

另请注意我是如何删除@前缀为Model的{​​{1}}。为了更好地理解原因,请参阅Introduction to ASP.NET Web Programming Using the Razor Syntax以及由Phil Haack撰写的同一主题的小参考here

答案 1 :(得分:1)

我认为@ john-h用他的答案击中了头部。但是,您可能希望降低为自己创建的复杂性。

1)由于ForceSimple和ForceAdvanced都是布尔值,因此假设当ForceAdvanced为真时,它不是“简单”,对吧?我不确定你在这里有什么其他的逻辑。

2)为什么不使用参数来设置搜索类型,而不是创建两个视图并“回发”以获取正确的视图?或者评估安全性以设置用户可以执行的安全性。这是一个例子:

控制器操作:

//id is the search type: true is Advanced
public ActionResult Search(bool id) {
    IndexViewModel viewModel = new IndexViewModel {
        /* Do whatever logic here  */
        ForceAdvanced = (id) ? false : true,
        AdvancedSearchOption = id
    };
    return View("search", viewModel);
}

[HttpPost]
public ActionResult Search(IndexViewModel model) {
    //model.SimpleSearchCriteria = "Testing";
    //model.PhoneNumberCriteria = "Phone";
    return View("search", model);
}

搜索视图:

@using (@Html.BeginForm(new { id = @Model.AdvancedSearchOption })) {
    <div style="clear:left; float: left" class="SearchText">
        @Html.Label("What's your inquiry?:")
        @if (Model.AdvancedSearchOption) {
            <div>
                @* if you really want, load your partial views here *@
                <span>@Html.LabelFor(m => m.NameCriteria)</span>
                @Html.EditorFor(m => m.NameCriteria, new { style = "width: 400px" })
                <span>@Html.LabelFor(m => m.PhoneNumberCriteria)</span>
                @Html.EditorFor(m => m.PhoneNumberCriteria, new { style = "width: 400px" })
            </div>
        }
        else {
             @* if you really want, load your partial views here *@
            @Html.EditorFor(m => m.SimpleSearchCriteria, new { style = "width: 400px" })
        }        
    </div>
    <div>
         <input type="submit" value="Search" />
    </div>   
     @Html.HiddenFor(m => m.ForceAdvanced)
     @Html.HiddenFor(m => m.AdvancedSearchOption)
     @Html.HiddenFor(m => m.ForceSimple)
     @Html.HiddenFor(m => m.AccountNumberCriteria)
     @Html.HiddenFor(m => m.CustomerNumberCriteria)
     @Html.HiddenFor(m => m.NameCriteria)
     @Html.HiddenFor(m => m.PhoneNumberCriteria)    
}

答案 2 :(得分:0)

我曾尝试明确地将模型发送给部分人,但没有运气。我相信如果没有指定任何内容,部分视图默认获取父模型,所以我需要做的就是在我的部分中指定模型类型,我得到了信息。

我终于通过大量的反复试验弄明白了。我的问题是由于我尝试使用jQuery来发帖。您需要做一些其他事情来更新模型,这样做。一旦我将其更改并在帖子的表单上放置了一个输入控件,我就从控制器中的父视图和部分视图中获取了所有数据。