Question

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?

Was it helpful?

Solution

Try if this works:

 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");            
            de.Select.ExtraParams.Add(new {
                         Name = "id", 
                         Value ="App.MyCombo.getValue() == null ? '0' : App.MyCombo.getValue()",     
                         Mode = ParameterMode.Raw 
                    });
        })
        .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>)
            )
        )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top