我在登录控制器中使用以下代码将用户 JWT 访问令牌存储在响应 cookie 中
var returnedJwtToken = authenticationResponse.Content;
try
{
//Store a WebAPI JWT accesstoken in an FormsAuthenticationTicket userData
var ticket = new FormsAuthenticationTicket( 1,
login.UserName,
DateTime.Now,
DateTime.Now.Add(TimeSpan.FromSeconds(returnedJwtToken.ExpiresIn)),
login.RememberMe,
returnedJwtToken.AccessToken,
FormsAuthentication.DefaultUrl);
//Encrypt it
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
//Add it to Response.Cookies
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) {
Domain = FormsAuthentication.CookieDomain,
Path = FormsAuthentication.FormsCookiePath,
HttpOnly = true,
Secure = FormsAuthentication.RequireSSL };
Response.Cookies.Add(cookie);
Run Code Online (Sandbox Code Playgroud)
然后我使用自定义的 MVC AuthorizeAttribute 来恢复访问令牌并将其放入请求标头中,以便可以在控制器上检索以将经过身份验证的请求发送到 WebAPI。它还为我的 MVC 应用程序控制器提供授权。
public class SiteAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase …Run Code Online (Sandbox Code Playgroud) 我正在使用引导程序模板,其复选框模板如下所示:
<div class="checkbox">
<label>
<input type="checkbox" class="checkbox style-1" checked="checked">
<span>Checkbox 1</span>
</label>
</div>
Run Code Online (Sandbox Code Playgroud)
我需要一个布尔值的MVC EditorTemplate才能使用此模板。MVC CheckBoxFor默认模板不同于此模板,它具有另一个隐藏的输入字段来保存数据,并且没有跨度来显示复选框图标(它使用css无法样式化的默认图标)。
MVC CheckBox对于默认模板:
<input checked="checked" data-val="true" data-val-required="required." id="IsActive"
name="IsActive" type="checkbox" value="true">
<input name="IsActive" type="hidden" value="false">
Run Code Online (Sandbox Code Playgroud)
我尝试了许多方法来完成此操作,但均未成功。例如,如果我使用如下所示的条件模板,则提交后它不会返回值。
我的布尔编辑器模板:
@model Boolean?
<div class="checkbox">
<label>
@if (Model.Value)
{
<input id="@ViewData.TemplateInfo.GetFullHtmlFieldId("")" name="@ViewData.TemplateInfo.GetFullHtmlFieldId("")"
type="checkbox" class="checkbox style-1" checked="checked" value="true" />
}
else
{
<input id="@ViewData.TemplateInfo.GetFullHtmlFieldId("")" name="@ViewData.TemplateInfo.GetFullHtmlFieldId("")"
type="checkbox" class="checkbox style-1" value="false" />
}
<span>@Html.LabelFor(m => m)</span>
</label>
</div>
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
更新:
部分css代码将相关事件关联到复选框图标:
label input[type=checkbox].checkbox + span:before {
font-family: FontAwesome;
font-size: 12px;
border-radius: 0; …Run Code Online (Sandbox Code Playgroud) asp.net checkbox asp.net-mvc mvc-editor-templates twitter-bootstrap
我有一个使用模型创建下拉列表的prtialview。在我的主视图中,我创建了partialview html字符串,以便稍后在javascript函数中使用它。
视图:
@model SomeViewModel
@{
var devicesDropDown = @Html.Partial("_DropDown", (SelectList)(Model.DeviceTypes));
var devicesHtmlDropDown = MvcHtmlString.Create(devicesDropDown.ToString());
}
Run Code Online (Sandbox Code Playgroud)
但是,当我想以这种方式在javascript中使用它时,它就像是导致javascript函数出错的未连接字符串。
js函数位于页面的底部:
<script type="text/javascript">
function format(d) {
var ddl = '@( devicesHtmlDropDown)';
return ddl;
}
</script>
Run Code Online (Sandbox Code Playgroud)
这是运行时字符串的样子:
var ddl = '<select class="form-control input" id="SelectedValue" name="SelectedValue" style="width: 100%">
<option selected="selected" value="456413">1</option>
<option value="564655465">2</option>
</select>
';
Run Code Online (Sandbox Code Playgroud)
我应该如何格式化此字符串以使其可以像静态html对象一样呈现。以及如何检索此下拉菜单的选定值以发布到操作?我应该使用TagBuilder吗?
javascript asp.net-mvc partial-views tagbuilder mvchtmlstring