c# - Set a default value to the model being passed to Partial View -
i have partial view called partial view (kind of nested partial views).
the outer partial view called company , inner partial view custom control called searchhelp. both accept parameter.
now company view gets parameter of type company , searchhelper accepts optional string. part works fine testing model value null , assigning default text @((model==null)?"enter text":model)
when used in other views without passing parameter.
in case of nested views, if dont provide string model searchhelper takes company
model outer view i.e company , gives error.
you can assign default value string-as-model it's called in view:
//null coalesce default string value: @html.partial("searchhelp", model.searchhelp ?? "default value")
...though might better using htmlhelper, can define default value 1 time:
public ihtmlstring searchhelp(this htmlhelper html, string searchhelp = "default value") { // make html here }
then
@html.searchhelp(model.searchhelp);
Comments
Post a Comment