tags:

views:

67

answers:

1

Hi everyone I have a LogOn view look like this

<%= Html.TextBox("username", "", new { @style = "width:149px;border:1px solid #CCC" })%>
<%= Html.Password("password","",new { @style = "width:149px;border:1px solid #CCC" }) %>
<%= Html.DropDownList("SrvId", (SelectList)ViewData["ServerID"], new { @style = "width:149px;border:1px solid #CCC; font-family:'Trebuchet MS';" })%>

Basicly the above view is the template from ASP.NET MVC, I just add the Html.DropDownList that i'll use to choose the connection string

Below is the Account Controller

public ActionResult LogOn()
{
    var list = new[] {
        new ServerID { Id = 0, Name = "<Please Select>" }, 
        new ServerID { Id = 1, Name = "Server1" }, 
        new ServerID { Id = 2, Name = "Server2" }, 
    };

    var selectList = new SelectList(list, "Id", "Name", 0);
    ViewData["ServerID"] = selectList;

    return View();
}

[AcceptVerbs(HttpVerbs.Post)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
    Justification = "Needs to take same parameter type as Controller.Redirect()")]
public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)
{
    if (!ValidateLogOn(userName, password))
    {
        return View();
    }

    FormsAuth.SignIn(userName, rememberMe);
    if (!String.IsNullOrEmpty(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Customer");
    }
}

This is the model which use to choose the Connection string that i got from internet

public partial class dbServiceModelDataContext : System.Data.Linq.DataContext
    {
        // Connection String Keys - stored in web.config
        static string PrimaryConnectionStringKey = "SRVConnectionString";
        static string SecondaryConnectionStringKey = "SRVConnectionString2";

        protected static string ConnectionS
        {
            get
            {

                if (##how to pass the dropdownlist value here ??##)
                {
                    return global::System.Configuration.ConfigurationManager.ConnectionStrings[PrimaryConnectionStringKey].ConnectionString;
                }
                else
                {
                    return global::System.Configuration.ConfigurationManager.ConnectionStrings[SecondaryConnectionStringKey].ConnectionString;
                }
            }
        }

        public dbServiceModelDataContext() :
            base(ConnectionS, mappingSource)
        {
            OnCreated();
        }
    }

my Question is how to pass value of DropDownList to the model ? I put my Connection String in Web.config Thanks..

A: 

if the dropdown is generated correctly you should be able to do it like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOn(all yourstuff, string[] SrvId)
{
    var id = (int)SrvId[0];
    ...
}
Omu
thank You for answering me, Sir but I still confuse how to send the value of SrvId to the model..
K0RBAN.M0D
@K0RBAN.M0D the id is the selected value from your dropdown, now just set in any way you need it
Omu
@Omu : hmm..I see, Sir.. I just need to place [AcceptVerbs(HttpVerbs.Post)] to pass the SrvId value in my model, is it right, Sir?
K0RBAN.M0D
@K0RBAN.M0D if your dropdown is wrapped in a form with method="post" yes, otherwise no
Omu