标签: asp.net-mvc-5

MVC 5仅在Home Index上显示Layout中的Partial-View

我在我的网站上使用侧边栏,其中包含来自我的数据库的统计数据和静态数据,如链接和其他文本.

在我_Layout.cshtml,我Html.RenderAction("SidebarPV", "Home");用来调用侧边栏.

侧边栏是使用ViewModel进行统计的部分视图. SidebarPV在我的HomeController喜欢中生成:

public ActionResult SidebarPV() {
    SidebarViewModel viewmodel = new SidebarViewModel();
    DateTime now = DateTime.Now;
    viewmodel.stat_data1 = db.Table1.Where(e => e.DateDeb <= now && e.DateFin >= now).Count();
    viewmodel.stat_data2 = db.Table2.Where(c => c.DateDeb <= now && c.DateFin >= now).Count();
    return PartialView("SidebarPV", viewmodel);
}
Run Code Online (Sandbox Code Playgroud)

它就像一个魅力,但我不需要所有视图的统计数据,只有 /Home/Index

因此,当ser不在网站索引上时,我想"评论"统计数据生成.

谢谢你的建议.

编辑(解决方案,感谢krillgar):

我写的是我的 _Layout

@{
    var isHome = ViewContext.RouteData.Values["controller"].ToString().ToUpper() == "HOME";
    var isIndex = ViewContext.RouteData.Values["action"].ToString().ToUpper() == "INDEX";
    if (isHome && isIndex) {
        Html.RenderAction("SidebarPV", "Home"); …
Run Code Online (Sandbox Code Playgroud)

c# partial-views razor asp.net-mvc-5

1
推荐指数
1
解决办法
4337
查看次数

MVC5 Ajax PartialView

我需要使用Ajax.BeginForm用PartialView刷新div.我在MVC4中已经完成了十几次这样做,它完美无缺.在MVC5中,虽然不起作用:(

以下是我采取的步骤:

  • 在Visual Studio 2013中创建一个新项目"PartialAjax"(ASP.NET Web应用程序/ MVC)
  • 右键单击Views/Home并添加视图>选中"Create as partial view">将其命名为"_Test"
  • 此_Test.cshtml视图的代码:

    <p>From now on I'm gonna change my life: @DateTime.Now.ToString()</p>
    
    Run Code Online (Sandbox Code Playgroud)
  • 编辑Views/Home/Index.cshtml视图:

    @{
                ViewBag.Title = "Home Page";
    }
    @using (Ajax.BeginForm("ChangeLife", "Home", null, new AjaxOptions() { UpdateTargetId = "test", HttpMethod = "Post" }, null))
    {
        <input type="submit" value="Start" />
    }
    
    <div id="test">
        @Html.Partial("_Test")
    </div>
    
    Run Code Online (Sandbox Code Playgroud)
  • 把它放在你的HomeController中:

    public ActionResult ChangeLife()
          {
              return this.PartialView("_Test");
          }
    
    Run Code Online (Sandbox Code Playgroud)

如果单击Manage NuGet packages,默认情况下会安装jQuery和Microsoft jQuery Unobtrusive Validation.

ajax jquery partial-views unobtrusive-validation asp.net-mvc-5

1
推荐指数
1
解决办法
4648
查看次数

EF Code First Fluent API - 级联删除

我有两个型号:

public class User
{
   .....
   public virtual UserProfile UserProfile { get; set;}
}


public class UserProfile
{
   .....
   public virtual User User { get; set;}
}
Run Code Online (Sandbox Code Playgroud)

User是主表和关系是一一对应的.一个用户只有一个UserProfile.

如何使用EF CodeFirst Fluent API定义User和UserProfile之间的关系,以便当我从User表中删除一个用户时,Userprofile中的用户配置文件也会被删除?

entity-relationship entity-framework asp.net-mvc-5 asp.net-identity-2

1
推荐指数
1
解决办法
4183
查看次数

上传的文件未显示在Project解决方案中(Mvc5)

我已实现以下代码来上传文件.该文件将上载到该位置("../App_Data/uploads"),但它不会显示在项目中.我必须在项目中手动包含该文件.为什么文件没有显示?

 public ActionResult ChangeSetting(SettingViewModel setting)
    {
        string userId = User.Identity.GetUserId();
        ApplicationUser currentUser = this._appUserRepo.Find(e => e.Id.Equals(userId)).FirstOrDefault();

        if (setting.PictureUrl != null)
        {
            if (setting.PictureUrl.ContentLength > 0)
            {
                string fileName = Path.GetFileName(setting.PictureUrl.FileName);
                if (fileName != null)
                {

                    string path = Path.Combine(Server.MapPath("../App_Data/uploads"), fileName);
                    setting.PictureUrl.SaveAs(path);
                    if (currentUser != null)
                    {
                        currentUser.PictureUrl = path;
                        currentUser.PictureSmalUrl = path;
                        currentUser.PictureBigUrl = path;
                    }
                }
            }
        }
        if (setting.FirstName != null)
        {
            if (currentUser != null) currentUser.FirstName = setting.FirstName;
        }           
        _appUserRepo.Update(currentUser);
        return RedirectToAction("index", "Admin");
    }
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-mvc-5

1
推荐指数
1
解决办法
1002
查看次数

当模型更新时,MVC 5脚手架仍使用旧模型

我先使用MVC 5,EF 6数据库和脚手架

我创建了一个实体数据模型,然后使用脚手架生成了控制器

然后,我需要更改数据库上的某些字段。我删除了更改的模型(因为EF仅检查数据库中的新事物,如果不删除模型,它将不会更新任何内容)

我删除了生成的文件并再次生成,但是它仍然使用旧的数据模型来生成我的控制器。

我尝试使用新模型(以前从未生成过控制器),但没有用。

你有什么建议吗?还是我更新模型的方式是错误的?

谢谢。

scaffolding asp.net-mvc-5

1
推荐指数
1
解决办法
962
查看次数

如何允许具有管理员权限的用户访问控制器

我有一个应用程序,我使用Windows身份验证.没有太详细,我的应用程序设置了一系列用户,然后给予管理员权限来创建缺席.如果他们没有管理员权限,那么他们就无法更改用户或创建缺席.

我想根据数据库中的admin标志是否设置为true来限制对某些控制器/操作的访问.我工作的用户属于多个组,没有管理员组,我可以将其包含在Authorize属性角色字符串中.

我在这里遵循了教程,但由于我有一个数据库第一实体框架模型,实体类继承自DbContext而不是来自身份上下文.

当我运行应用程序时,我的代码引发了一个错误说:"mscorlib.dll中发生了'System.InvalidOperationException'类型的异常但未在用户代码中处理

附加信息:实体类型IdentityRole不是当前上下文模型的一部分."我点击查看详细信息,我看到这个"实体类型IdentityRole不是当前上下文模型的一部分."

这是发生错误的代码片段:

        AbsencesEntities context = new AbsencesEntities();

        AbsenceRepository absenceRepository = new AbsenceRepository(context);
        IdentityResult IdRoleResult;
        IdentityResult IdUserResult;

        // Create a RoleStore object by using the UserSecurity object.
        // The RoleStore is only allowed to contain IdentityRole objects.
        var roleStore = new RoleStore<IdentityRole>(context);

        // Create a RoleManager object that is only allowed to contain IdentityRole objects
        // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore
        var roleMgr = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc entity-framework asp.net-mvc-5

1
推荐指数
1
解决办法
860
查看次数

Sitecore 7.2和MVC 5,没有视图渲染

从我读到的内容,Sitecore 7.2对MVC(5)有原生支持,我刚安装了一个新版本的Sitecore 7.2,在同一台机器上我安装了ASP.net 4.5和MVC 3,4和5.SQL Server也在同一台机器上运行(SQL Server 2008 R2 SP1),但是一旦我尝试在sitecore中创建新的渲染,就没有选项可以使用"views/razor views".我只能选择使用.ascxxslt.

我错过了某种隐藏的配置吗?是否需要在同一台计算机上安装Visual Studio以支持MVC?

最好的问候,Inx

asp.net-mvc sitecore sitecore7 asp.net-mvc-5 sitecore7.2

1
推荐指数
1
解决办法
611
查看次数

MVC 5在IIS 7中不起作用

我尝试在IIS7中运行我的ASP.NET MVC5,但发生的是网站只显示我的cshtml文本.

我的配置:

GoWireless>基本设置>选择(.Net Framework版本:无管理控制台,管道模式:经典)

GoWireless>目录浏览>已启用

Index.cshtml仅显示此文本

@model IEnumerable @ {ViewBag.Title ="Active Directory"; } 活动目录

欢迎,@ User.Identity.Name.Remove(0,User.Identity.Name.IndexOf("\")+ 1)!

这是GoWireless活动目录搜索器,您可以使用搜索框或单击左下角的某个员工来查看员工详细信息.如果要搜索仅存在于GoWireless\ActiveDirectory中且不存在于GW_UTA\ActiveDirectory2中的用户,则可以将其完整格式的其详细信息(SamAccountName,GivenName,Surname,Email或EmployeeNumber)输入并搜索到搜索框中.

学到更多

_Layout.cshtml仅显示此文本

@model IEnumerable
Toggle navigation   Failed to load images
@User.Identity.Name
@using (Html.BeginForm("Index", "Home", FormMethod.Post)) {
@Html.TextBox("search", null, new { @class = "form-control", @placeholder = "Search..." })  
}
@foreach (var item in Model) {
class="active"} href='@Url.Action("Details", "Home", new { id = item.SamAccountName.Replace(".", "_") })'>@item.SamAccountName
ViewBag.count = 1; } @if (ViewBag.count != 1) { EasyAD.EasyAD ad = new EasyAD.EasyAD("dc1.gowireless.net:389", "gowireless\\ldapuser", "abc123!@#"); System.Data.DataTable …
Run Code Online (Sandbox Code Playgroud)

c# iis-7 visual-studio-2012 asp.net-mvc-5

1
推荐指数
1
解决办法
9509
查看次数

使用其他bool字段进行MVC远程验证

我正在尝试使用远程验证和其他bool复选框字段

[Remote("IsStorageConnectionValid", "TenantManagement", AdditionalFields = "CreateStorage")]
public String StorageConnectionString { get; set; }
Run Code Online (Sandbox Code Playgroud)

验证码

public JsonResult IsStorageConnectionValid(string storageConnectionString, bool createStorage){
Run Code Online (Sandbox Code Playgroud)

它在击中验证器方面非常有效.但是,无论复选框的值如何,createStorage始终为true.如果我使用不是复选框的其他字段,则可以完美地提供它们.

复选框作为标准创建:

  @Html.CheckBoxFor(m => m.CreateStorage)
Run Code Online (Sandbox Code Playgroud)

这是一个错误吗?或者我做错了吗?

小提琴在这里(MVC4我认为但做同样的事情)

asp.net-mvc unobtrusive-validation asp.net-mvc-5 asp.net-mvc-5.2

1
推荐指数
1
解决办法
1157
查看次数

提交ajax表单时,MVC 5模型属性为null

我创建一个模型并将其传递给局部视图.当我提交模型ModelStat.IsValid为true时,无论我在表单上输入什么值,其属性都为null.

控制器和型号

public class TestController : Controller
{
    // GET: Test
    public ActionResult Index()
    {
        TestModel model = new TestModel();
        model.SomeFieldName= "Test";
        model.OtherFieldName = "AnotherTest";
        return PartialView(model);
    }
    [HttpPost]
    public PartialViewResult Index(TestModel model)
    {
        if(ModeState.IsValid)
        {
            //Do Stuff to model
        }
        return PartialView(model);
    }
    public class TestModel
    {
        [Required]
        public string SomeFieldName;
        [Required]
        public string OtherFieldName;
    }
}
Run Code Online (Sandbox Code Playgroud)

局部视图

@model Portal.Controllers.TestController.TestModel
@using (Ajax.BeginForm("Index", new AjaxOptions { UpdateTargetId = "Content" }))
{
    @Html.ValidationSummary(true)
    <div id="Content">
       @Html.LabelFor(model => model.SomeFieldName,"FieldName")
       @Html.TextBoxFor(model => model.SomeFieldName) …
Run Code Online (Sandbox Code Playgroud)

ajax jquery asp.net-mvc-5

1
推荐指数
1
解决办法
2095
查看次数