Tag: asp.net mvc 4

如何在自定义模型绑定器中validation我的模型?

我在这里询问了我用逗号分隔的数值的问题。 鉴于一些回复,我试图尝试实现我自己的模型绑定器,如下所示: namespace MvcApplication1.Core { public class PropertyModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { object objectModel = new object(); if (bindingContext.ModelType == typeof(PropertyModel)) { HttpRequestBase request = controllerContext.HttpContext.Request; string price = request.Form.Get(“Price”).Replace(“,”, string.Empty); ModelBindingContext newBindingContext = new ModelBindingContext() { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType( () => new PropertyModel() { Price = Convert.ToInt32(price) […]

在控制器post方法中,MVC 4模型中的提交表单为空

所以我现在的问题是,当我提交以下表格时,我无法将我的模型放入我的控制器中。 我试图让BillingCodes(这是一个BillingCodeObjects列表)中的项循环并显示。 我已经删除了一些与这种情况无关的代码,使其更简单,更容易阅读。 这是我的观点代码…… @using (Html.BeginForm(“SubmitTimesheet”, “Timesheet”, FormMethod.Post)) { foreach (var item in Model.BillingCodes) { @item.Name @item.TotalHours Enter Time: @Html.DropDownListFor(model => item.EnterTimeHours, new SelectList(new[] { new { Value = “0”, Text = “0” }, new { Value = “1”, Text = “1” }, new { Value = “2”, Text = “2” }, new { Value […]

MVC中的会话管理

我是MVC的新手。 我正在MVC4 Razor中创建新的WebApplication。 我想维护所有页面的用户登录会话。 任何人都可以用小例子解释如何在MVC中维护所有视图的会话。

Web Api不会使用jQuery Ajax和Basic Auth下载文件

我正在使用ASP.NET Web API构建Web服务(和站点)的原型,该原型具有下载文件的方法。 当前端的用户按下导出按钮时,控制器发出并接收jQuery ajax GET请求,然后控制器调用名为Excel的方法(如下所示)。 该方法运行没有任何问题并完成。 当我在标题中查看Chrome时(请参阅https://skydrive.live.com/redir?resid=2D85E5C937AC2BF9!77093 ),它会收到响应(据我所知)所有正确的标题。 我正在使用Basic Auth,因此使用http授权头传输用户凭据,我已使用Ajax选项手动将其添加到每个jQuery Ajax请求中。 var excelRequest = $.ajax({ url: ‘http://localhost:59390/api/mycontroller/excel’, cache: false, type: ‘GET’, data: gridString, dataType: ‘json’, contentType: ‘application/json; charset=utf-8’ }); $.ajaxSetup({ beforeSend: function (xhr) { SetAuthRequestHeader(xhr) } }); function SetAuthRequestHeader(jqXHR) { var usr = “Gebruiker2”; // TODO: Change, this is for testing only. var pw […]

asp.net mvc中的LINQ + EntityFunctions

我有这样的代码使用EntityFramework Alpha3(来自nuget): class Member { [Key] public int Key { get; set; } public string Forename { get; set; } public string Surname { get; set; } public string Sitename { get; set; } public DateTime? RegDate { get; set; } } class MembersContext : DbContext { public MembersContext() : base(“Name=ConnectionString”) { } public DbSet […]

如何将值从一个视图传递到另一个视图

我是一个非常新的ASP.NET使用MVC控制器…所以这是我的场景。开发一个在线入学系统,学生来填写他们所需的记录,以便进行处理。我有不同的类,其中包含有关我的学生class是: public class Student { public int ID { get; set; } [Required] public string NAME { get; set; } [Required] public string FIRSTNAME { get; set; } [Required] public string LASTNAME { get; set; } public virtual ICollection Enrollments { get; set; } } 我有另一个名为Enrollments的课程,其中用户输入所有课程以及成绩 public class Enrollment { public int EnrollmentID { get; […]

使用Max + 1创建用户友好的唯一序列号的替代方法

我在我的asp.net mvc web应用程序中有了存储库方法,以自动生成一个名为Tag的唯一序列号: – public void InsertOrUpdateServer(TMSServer server, string username) { if (server.TMSServerID == default(int)) { // New entity int technologyypeID = GetTechnologyTypeID(“Server”); Technology technology = new Technology { IsDeleted = true, Tag = “S” + GetTagMaximumeNumber(technologyypeID).ToString(), StartDate = DateTime.Now, }; Save(); //code goes here GetTagMaximumeNumber()函数的位置是: – public int GetTagMaximumeNumber(int typeID) { string s = […]

表单validation失败时,将错过查询字符串参数

我有一个包含以下url的表单:CreateEntity? officeCodeId = 5 当我发送表单进行validation并且validation失败时,它只返回CreateEntity url。 没有officeCodeId = 5。 如果用户点击URL或F5上的输入 – 我的网站失败 – 它需要丢失officecodeId参数。 我可以将它保存到会话或其他存储中。 但是我希望在URL中有它 我的看法: [HttpGet] public virtual ActionResult CreateEntity(int? officeCodeId) { var model = new CreateViewModel(); FillViewModel(model, officeCodeId); return View(“Create”, model); } [HttpPost] protected virtual ActionResult CreateEntity(TEditViewModel model) { if (ModelState.IsValid) { //Do some model stuff if } return View(“Create”, model); } […]

如何使用DropDownList绑定到WebGrid razor asp net mvc 4内的模型?

经过几个小时的谷歌搜索和尝试不同的语法,我决定发布这个。 问题是我无法在webgrid中使用DropDownList。 考虑以下模型: public class UsuariosModel { public int UserId { get; set; } public string UserName { get; set; } public string Nombres { get; set; } public string ApellidoPaterno { get; set; } public string ApellidoMaterno { get; set; } public string Titulo { get; set; } public string Telefono { get; set; […]

将IEnumerable列表传递给控制器

在我的asp.net MVC 4项目中,我正在尝试将IEnumerable列表从视图传递到控制器。 问题是在动作中接收的列表为空。 请帮忙。 这是我的代码的一部分 观点: @model IEnumerable @using (Ajax.BeginForm(“AddNewLeader”, “Equipe”, FormMethod.Post, new AjaxOptions { UpdateTargetId = “Leader”, HttpMethod = “Post”, InsertionMode = InsertionMode.Replace })) { foreach (var item in Model) { @Html.HiddenFor(modelItem => item.ID_agent) @Html.RadioButtonFor(modelItem => item.SelectedAgent, item.ID_agent) @Html.DisplayFor(modelItem => item.nom_agent) @Html.DisplayFor(modelItem => item.prenom_agent) } } 控制器: public ActionResult AddNewLeader(IEnumerable ListAgent) { […] […]