在普通的ASP.NET Web表单站点中,我将使用web.configs"appsettings"将应用程序设置数据添加到站点.但是,使用MVC 3时,我无法以这种方式检索设置值.
首先,有2个web.config文件.一个位于站点的根目录中,第二个列在"视图"区域中.我假设我想将我的appsettings信息放在root web.config文件中,对吗?(将其置于其他视图下似乎会产生错误,指出"AppSettings"每个Web应用程序只能出现一次.)
当我尝试检索它时(C#:System.Configuration.ConfigurationManager.AppSettings ["SettingName"])我得到一个空白或空/ null返回值.我究竟做错了什么?
我应该提一下,我实际上是在模型区域下的类文件中检索此信息,以获取使用get的模型的特定值集; 组;.我有可能不允许在模特中这样做吗?
在Controller.cs中:
WindowsLiveConnect.ServiceConfiguration WLSC = new WindowsLiveConnect.ServiceConfiguration();
ViewBag.ClientID = SC.ClientID; // This returns empty
Run Code Online (Sandbox Code Playgroud)
在web.config中
...
<appSettings>
<add key="webpages:Version" value="1.0.0.0"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="ClientID" value="0000000040062A3F" />
<add key="ClientSecret" value="SUPERSECRETPASSWORD" />
<add key="RedirectURL" value="http%3A%2F%2Fwww.quilnet.com" />
</appSettings>
...
Run Code Online (Sandbox Code Playgroud)
在Model.cs文件中:
public class ServiceConfiguration
{
private string clientid;
private string clientsecret;
private string redirecturl;
public string ClientID
{
get { return clientid; }
set
{
clientid = System.Configuration.ConfigurationManager.AppSettings["ClientID"];
}
}
public …Run Code Online (Sandbox Code Playgroud) 我在将模型绑定到MVC中的列表时遇到问题.我的页面具有动态添加和删除文本框的功能.我的页面HTML将是
<form id="prdt">
<div id="div_0"> <input type="text" name="products[0].Name" value="Coffee"/><button id="0" onclick="return DeleteLocation(this.id)">Delete</button></div>
<div id="div_1"> <input type="text" name="products[1].Name" value="Tea" /><button id="1" onclick="return DeleteLocation(this.id)">Delete</button></div>
<div id="div_2"> <input type="text" name="products[2].Name" value="Cola" /><button id="2" onclick="return DeleteLocation(this.id)">Delete</button></div>
<div id="div_3"> <input type="text" name="products[3].Name" value="Pepsi" /><button id="3" onclick="return DeleteLocation(this.id)">Delete</button></div>
</form>
Run Code Online (Sandbox Code Playgroud)
下面是删除文本框的代码
<script type="text/javascript">
function DeleteLocation(id) {
$("#div_" + id).remove();
}
</script>
Run Code Online (Sandbox Code Playgroud)
但是当我删除"可乐"文本框并做一个ajax帖子时,我的列表中只有咖啡和茶(Controller Action Post).即列表中省略了最后一个
同样,当我删除"茶"文本框并做一个ajax帖子时,我只能获得咖啡.即列表中排除了其他三个值.
我认为列表与List索引绑定.是否有任何方法可以获取所有值,即使其中的任何项目被删除.
我需要在我的MVC应用程序中实现母版页asp.net概念,当我用Google搜索时,我可以理解可以通过使用MVC中的布局来获得相同的内容。
我的应用程序的〜Views \ Shared_Layout.cshtml下有_Layout文件。
为了将内容包含在我的员工详细信息页面的布局文件中,我添加了以下内容:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "DisplayEmployeeList";
}
Run Code Online (Sandbox Code Playgroud)
我想知道,是否要在所有页面中单独重复上面的代码,还是可以在全局某处重复执行,以便在所有页面上都可以看到它。