asp.net-mvc-5

EF Code First Fluent API - Cascade Delete

那年仲夏 提交于 2019-12-06 15:35:04
I have two models: public class User { ..... public virtual UserProfile UserProfile { get; set;} } public class UserProfile { ..... public virtual User User { get; set;} } The User is the master table and the relation is one to one. One user has only one UserProfile. How can I define the relationship between User and UserProfile using EF CodeFirst Fluent API in such a way that when I delete one user from User table the user profile from Userprofile is also deleted? Use WillCascadeOnDelete modelBuilder.Entity<UserProfile>() .HasKey(c => c.Id) .HasRequired(c => c.User) .WithRequiredDependent(c =

AsP.Net MVC 5 how to call a function in view

心已入冬 提交于 2019-12-06 15:20:47
I have ASP.Net MVC 5 application I want to call a method from view how can I do it? my code: My UsersList function: public ActionResult UsersList() { var User_VM = new UserVM { MyUsers = context.Users.OrderBy(u => u.Email).Include(u => u.Roles).ToList() }; return View(User_VM); } UsersList View: @foreach(var user in Model.MyUsers) { <tr> <td>@user.Email</td> <td> @foreach(var r in user.Roles) { <p> @Html.Action(GetRoleNameById(r.RoleId)) </p> } </td> </tr> } and my function in controller: public ActionResult GetRoleNameById(string RoleId) { var RoleName = context.Roles.Where(r => r.Id ==

Passing a variabe to jquery using Odometer

随声附和 提交于 2019-12-06 14:54:26
问题 I would like to use a jquery odometer to display information on a master page. http://www.jqueryscript.net/animation/Smooth-Animated-Numbers-with-Javascript-CSS3-odometer.html In order to do that I have to retrieve that value from SQL Server using C#. Then I have to pass it to the jscript odometer in the html() as shown below. If I get the valuje - how do Isend it to the javascript? <script> setTimeout(function(){ $('.odometer').html('123222'); }, 1000); </script> 回答1: You can use the below

mailto link encoding in MVC Razor

﹥>﹥吖頭↗ 提交于 2019-12-06 14:51:03
I have the following model public class EmailLinkModel { public string mailbody { get; set; } public string emailSubject { get; set; } public string emailto { get; set; } } mailbody contains the actual text which is a part of mail body. This text contains a long text [ contains special characters like ? < etc and can contain line breaks spaces etc] I want to create a mailto html tag in the partial view and i tried 3 different approaches and at some point my mailto link not opening the default mail client I think the reason was Html or Url ecnoding [ I am showing the mailbody content in a text

ASP.NET Boilerplate MVC 5.x template fails to start

主宰稳场 提交于 2019-12-06 14:38:28
After downloading ASP.NET MVC 5.x template, I update the connection string, restore NuGet packages and then try to run Update-Database command in Package Manager Console, but I consistently get the following error: Could not load file or assembly 'Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) I was able to solve this once by removing all packages and adding them back one at a time, but this is extremely time

How to write unit test code for my ADO.Net based repository from VS2013

北慕城南 提交于 2019-12-06 14:38:14
问题 i am quite new in unit test area. so please guide me how to write unit test for ADO.Net based repository and action method inside my controller? i will be using VS own unit test framework. so please see the code and tell me what are the classes and functions need to go through unit test? it will be really helpful if some one tell me which area i need to unit test and which area not required? if possible please write few unit test code which show me how to write unit test for action ,student

Create a complex type model validation attribute with server and client validation

人盡茶涼 提交于 2019-12-06 14:32:01
I'm trying to create an attribute that can validate a complex type both on the server and client side. This attribute will be used for required and non required complex types such as the following Address Class public partial class AddressViewModel { [DisplayName("Address 1")] [MaxLength(100)] public virtual string Address1 { get; set; } [DisplayName("Address 2")] [MaxLength(100)] public virtual string Address2 { get; set; } [MaxLength(100)] public virtual string City { get; set; } [MaxLength(50)] public virtual string State { get; set; } [MaxLength(10)] [DisplayName("Postal Code")] public

Implement Unity MVC 5 dependency injection in ActionFilter

∥☆過路亽.° 提交于 2019-12-06 13:51:24
I'm trying to inject this user service via Unity (mvc5) in an actionfilter but it is null. How can I implement this? public class TestFilter : ActionFilterAttribute { // this is always null [Dependency] public IUserService UserService { get; set; } // other members } You must register UnityFilterAttributeFilterProvider as a FilterProvider first. Modify the App_Start > UnityMvcActivator 's Start method like this: public static void Start() { var container = UnityConfig.GetConfiguredContainer(); FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>()

Which NuGet package creates the web.config in your Views folder?

烂漫一生 提交于 2019-12-06 13:23:24
My project is missing the web.config file from the Views folder. Here are the steps I took to make the project. Created an empty MVC project and installed the packages listed below. Created a controller and view. The view was created by right-clicking the action name and selecting 'Add View...' from the menu. Attempt to run the project, but got the error CS0103: The name 'ViewBag' does not exist in the current context I am getting this error because web.config is missing from my Views folder. Since the web.config contains a lot of version numbers, tokens, etc..., my question is: Why wasn't

Identity UserManager.AddToRole throws exception

只谈情不闲聊 提交于 2019-12-06 13:07:59
问题 As the title says I am using the new C# MVC 5 Identity doing a simple call: UserManager.AddToRole(User.Id, User.RequestedRole); I am doing this in a method of my ViewModel that is called from the Controller The UserManager is created in the Base Class of my ViewModel like this: UserManager = new UserManager<TMUser>(new UserStore<TMUser>(new TMContext())); When I make the above call to AddToRole method, I get this Inner Exception (The outer one is generic/useless): {"A relationship from the