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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -