Render partial view with parameter

  1. #1

    Render partial view with parameter - if conditional in DirectEvents(?)

    I have a partial view on my mvc page. The view is rendered by default with no data, but will be updated based on a value selection from a combobox in another section of the page. The partial view takes an id as a parameter which it will use to get the data needed to return the model.

    The problem that I am having is that on the initial load, the parameter is null since nothing has been selected and I am getting a null value exception.
    Is there a way that I can use an if statement in a direct events call to check the selected item and return 0 is that is null?

    See me sample code below for clarification.

    Thanks

    Here are the relevant parts of my main page (index.cshtml) -

    x.ComboBox()
                            .ID("MyCombo")
                            .DisplayField("Title")
                            .ValueField("Number")                        
                            .TypeAhead(false)
                            .Width(500)
                            .PageSize(10)
                            .HideBaseTrigger(true)
                            .MinChars(0)
                            .TriggerAction(TriggerAction.Query)                        
                            .DirectEvents(de =>
                            {
                                de.Select.Url = Url.Action("MyPartial");
                                @*    Can I use an if statment here to check the selected item's value? *@                            
                                de.Select.ExtraParams.Add(new { id = App.MyCombo.getValue() });
                            })
                            .ListConfig(Html.X().BoundList()
                                .LoadingText("Searching...")
                                .ItemTpl(Html.X().XTemplate()
                                    .Html(@<text>
                                    <div class="search-item">
                                        <h3><span>{Number}</span>{Title}</h3>
                                        {Description}
                                    </div>
                                    </text>)
                                )
                            )
    
    ........
    
    @Html.Partial("MyPartial", Model.MyPartialVM)
    and here is my controller code -

    public ActionResult MyPartial(string id)
            {
                var vm = new MyPartialViewModel
                {
                    Number = id,
                    Title = "New Title"
                };
    
                ViewData.Model = vm;
    
                var pvr = new Ext.Net.MVC.PartialViewResult
                {
                    ViewData = this.ViewData
                    
                };
                return pvr;
            }
    This works if I hardcode a parameter value, but not if I try it as it is now. Here is the error I get -

    Message=Cannot perform runtime binding on a null reference

    So I was thinking that I can do an if in teh DirectEvents piece to check for a null on the combobox selection, I can inject a 0 when necessary and handle that in the controller. Can this be done?
    Last edited by czuroski; May 12, 2014 at 1:03 PM.
  2. #2
    I also posted this on stackoverflow (here) and was able to resolve it by changing -

    de.Select.ExtraParams.Add(new { id = App.MyCombo.getValue() });
    to

    de.Select.ExtraParams.Add(new Ext.Net.Parameter
                                {
                                    Name = "Id",
                                    Value = "App.MyCombo.getValue() == null ? '0' : App.MyCombo.getValue()",
                                    Mode = ParameterMode.Raw
                                });
    Thanks

Similar Threads

  1. Replies: 2
    Last Post: Feb 06, 2014, 1:19 PM
  2. Replies: 3
    Last Post: Dec 02, 2013, 1:01 PM
  3. [CLOSED] Partial View
    By PriceRightHTML5team in forum 2.x Legacy Premium Help
    Replies: 9
    Last Post: Jul 16, 2013, 1:16 PM
  4. [CLOSED] [2.1] MVC Partial View
    By softmachine2011 in forum 2.x Legacy Premium Help
    Replies: 4
    Last Post: Aug 23, 2012, 12:04 PM
  5. [CLOSED] Render Partial View on request failure
    By SymSure in forum 1.x Legacy Premium Help
    Replies: 8
    Last Post: Apr 01, 2011, 1:51 PM

Tags for this Thread

Posting Permissions