In the drop-down list of the stronugly typed view of Asp.net MVC, the selected items set in the controller are invalid in the view.
Below is myCode
Allunsendmail
[Acceptverbs (httpverbs. Post)]
Public Actionresult allunsendmail (formcollection collection)
{
Int Sysid = Convert. toint32 (Collection [ " Ddlsysid " ]);
Int Selectedval =- 1 ;
List < Systeminfoentity > Allsys = Systeminfoclient. getall ();
Allsys. insert ( 0 , New Systeminfoentity
{
Sysid = - 1 ,
Name = " Select "
});
If (Sysid > 0 )
{
Selectedval = Sysid;
}
Selectlist servicelist = New Selectlist (allsys, " Sysid " , " Name " , Selectedval );
Viewdata [ " Ddldatasysid " ] = Servicelist;
Viewdata [ " Subject " ] = String . Empty;
VaR viewdata = Messagequeries. getallunsendmails (sysid, 1 , 15 );
Return View (viewdata );
}
The following list of corresponding views
<% = Html. dropdownlist ( " Ddldatasysid " , (Selectlist) viewdata [ " Ddldatasysid " ]) %>
The problem is that the name of dropdownlist is the same as that of viewdata. You only need to modify the name as follows.
<% = Html. dropdownlist ( " Ddlsysid " , (Selectlist) viewdata [ " Ddldatasysid " ]) %>