开发者

Choose Connection String from view in ASP.NET MVC

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..


I would use an ViewModel and make the view strongly typed. The ViewModel would contain the list of connection strings to choose from as well as the value currently selected so that the view knows which one to mark as selected. You definitely should not try to find the connection string from within the view itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜