PDA

View Full Version : [CLOSED] [#74] combobox and default value in razor



zwf
Dec 05, 2012, 9:08 AM
Hi,

I searched the forum, but I don't get it to work :



Html.X().ComboBoxFor(m => m.ES_VERO_TYP)
.AllowBlank(true)
.Editable(false)
.FieldLabel("Verordnung")
.LabelAlign(LabelAlign.Left)
//.SetValueAndFireSelect(Html.X().ListItem().Text("Alle").Value("100").Mode(ParameterMode.Raw))
//.SelectedItems(Html.X().ListItem().Text("Alle").Value("100").Mode(ParameterMode.Raw))
.Items(
litems =>
{
litems.Add(
Html.X().ListItem().Text("Einzel").Value("1").Mode(ParameterMode.Raw)
);
litems.Add(
Html.X().ListItem().Text("Sammel").Value("2").Mode(ParameterMode.Raw)
);
litems.Add(
Html.X().ListItem().Text("Keine").Value("0").Mode(ParameterMode.Raw)
);
litems.Add(
Html.X().ListItem().Text("Alle").Value("100").Mode(ParameterMode.Raw)
);
}
)

I want the last ListItem with value 100 to be the default value .....

I don't get it ......

Regards,
Holger

zwf
Dec 05, 2012, 9:29 AM
.Listeners(li => {
li.Render.Handler = "this.select(this.getStore().getAt(3));";
})

Is a solution. But, isn't there a more obvious solution ?

Regards,
Holger

Daniil
Dec 05, 2012, 2:15 PM
Hi Holger,

This appears to be working for me.

.SelectedItems(Html.X().ListItem().Value("100").Mode(ParameterMode.Raw))

Example

<!DOCTYPE html>

<html>
<head>
<title>Ext.Net.MVC v2 Example</title>
</head>
<body>
@Html.X().ResourceManager()

@(Html.X().ComboBox()
.SelectedItems(Html.X().ListItem().Value("100").Mode(ParameterMode.Raw))
.Items(
Html.X().ListItem().Text("Item 1").Value("1").Mode(ParameterMode.Raw),
Html.X().ListItem().Text("Item 1").Value("2").Mode(ParameterMode.Raw),
Html.X().ListItem().Text("Item 1").Value("0").Mode(ParameterMode.Raw),
Html.X().ListItem().Text("Item 1").Value("100").Mode(ParameterMode.Raw)
)
)
</body>
</html>


Does the example work for you?

Daniil
Dec 05, 2012, 2:31 PM
It will look a bit different in a case with a ComboBoxFor.

ComboBoxFor gets a SelectedItem from a Model even it is null.

So, you can set up a SelectedItem in a Model or try this:


@(Html.X().ComboBoxFor(...)
.SelectedItems(si => {
si.Clear();
si.Add(Html.X().ListItem().Value("100").Mode(ParameterMode.Raw));
})
.Items(
Html.X().ListItem().Text("Item 1").Value("1").Mode(ParameterMode.Raw),
Html.X().ListItem().Text("Item 1").Value("2").Mode(ParameterMode.Raw),
Html.X().ListItem().Text("Item 1").Value("0").Mode(ParameterMode.Raw),
Html.X().ListItem().Text("Item 1").Value("100").Mode(ParameterMode.Raw)
)
)

Daniil
Dec 07, 2012, 10:59 AM
ComboBoxFor gets a SelectedItem from a Model even it is null.


We consider it as a defect.

Opened an issue to track it, see:
https://github.com/extnet/Ext.NET/issues/74

Vladimir
Dec 09, 2012, 9:45 PM
Can you provide type of 'ES_VERO_TYP' field and its value in the model?

zwf
Dec 10, 2012, 8:20 AM
Can you provide type of 'ES_VERO_TYP' field and its value in the model?

type of ES_VERO_TYP is int.
And the value in the model ? Please clarify, do you mean type ? The type in the model is int !

Regards,
Holger

Vladimir
Dec 10, 2012, 8:23 AM
And the value in the model ? Please clarify, do you mean type ?

You pass model object to the view in controller action, what value has 'ES_VERO_TYP' field in that model object?

zwf
Dec 10, 2012, 9:14 AM
You pass model object to the view in controller action, what value has 'ES_VERO_TYP' field in that model object?

It's int in the model


public int ES_VERO_TYP { get; set; }

Vladimir
Dec 10, 2012, 10:13 AM
Yes, I understood that type is int
I need to know what kind of value you set in model object


return this.View
{
new MyModelName
{
ES_VERO_TYP = 100 // in this sample the value is 100 but what value in your sample?
}
};


Also, please clarify do you have any value in combo (in input field) or it is blank (no selected items at all)?
It would be best if you can porvide controller code and view (just create simple test sample reproduces your original issue)