Tag: razor

使用cshtml的Visual Studio 2013 Autoformat(CTRL KD)小写问题

我刚刚更新到VS 2013,当使用MVC 3应用程序时,我遇到了一个自动格式化问题,当使用CTRL K + D快捷键(或突出显示)时,cshtml文件顶部的模型声明设置为小写所有,格式选择)例如 @model IEnumerable CTRL K + D. @model IEnumerable VS 2012中的相同项目完全正常并且自动格式化工作,所以我尝试导出这些设置并导入它们但没有任何运气。 我已经尝试查看工具 – 选项 – 文本编辑器并将html Web表单中的客户端标记更改为“已输入”,但这也没有奏效。

cshtml中的OpenFileDialog

我可以知道我可以在Razor中写这样的c#Opendialog吗? 我正在尝试制作一个openfiledialog,它可以让用户将照片上传到SqlServerCe数据库: OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = “c:\\”; openFileDialog1.Filter = “All files (*.*)|*.*”; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { string path = openFileDialog1.FileName; byte[] image = File.ReadAllBytes(path); string query = “UPDATE firma SET logo=@Image WHERE id = 1”; SqlCommand sqlCommand = new SqlCommand(query, conn); sqlCommand.Parameters.AddWithValue(“@Image”, image); […]

根据下拉选择显示/隐藏控件mvc 4 razor c#

这是我的代码 @Html.DropDownListFor(z => z.SelectedReportId, new SelectList(Model.ReportTypes, “Value”, “Text”, Model.SelectedReportId), “– Select Report –“) @Html.CheckBoxFor(model => model.IncludePhotos)@Html.LabelFor(model => model.IncludePhotos) 哪个产生: — Select Report — Excel Report Text Report 我有一个下拉列表和一个复选框,如果用户选择下拉列表中的第一个值,我需要禁用该复选框。 这是我使用的javascript没有成功 $(function () { $(‘#SelectedReportId’).change(function () { var value = $(this).val(); if (value == ‘1’) { $(‘#IncludePhotos’).show(); } else { $(‘#IncludePhotos’).hide(); } }); }); 感谢任何帮助,谢谢

对象引用未设置为对象(从View调用Razor模型)

使用C#MVC4 我的看法: @using Universe.Models @model UserModel @section css { } @using (Html.BeginForm(“AddUser”,”Home”, FormMethod.Post)) { Alias: @Html.TextBox(Model.Alias) 我的型号: public class UserModel { public int Id { get; set; } public string Alias { get; set; } public string Email { get; set; } public string Password { get; set; } public bool IsExternal { get; set; […]

找不到为MVC 5 DropDownListFor添加占位符的方法

我尝试在网上搜索并尝试使用我的代码进行不同的操作。 我知道如何为文本框添加占位符,但如何为MVC 5下拉列表添加一个? 我有以下代码,但不会将占位符放在下拉列表中。 @Html.DropDownListFor(model => model.CustomerID, new SelectList(ViewBag.Customers, “CustomerID”, “Email”, null, new { htmlAttributes = new { @class = “form-control”, @placeholder = “-Select-” } })) 我需要的语法是什么?

从c#SelectList中获取文本项

使用Visual Studio Express 2012 for Web和Razor,我创建了一个选择列表: List list = new List(); list.Add(new SelectListItem { Text = “Yes”, Value = “1” }); list.Add(new SelectListItem { Text = “No”, Value = “2” }); SelectList selectList = new SelectList(list, “Value”, “Text”, null); 后来,我想获得与selectList中的特定元素相关联的文本。 作为一个新手,我想我可以做到这一点: selectList.Items[1].Text 但这会导致消息“无法将带有[]的索引应用于类型为’System.Collections.IEnumerable’的表达式” 谢谢。

MVC提交按钮未触发

我正在使用带有Razor引擎的ASP.net MVC 4。 我有一个页面(Index.cshtml)和一个控制器(HomeController.cs) 我正在尝试将我的提交按钮连接到我的控制器中的操作结果 – 但是我似乎无法解决它。 我的HTML: @using (Html.BeginForm(“SubmitForm”, “Home”, FormMethod.Post)) { ******lots of other html here******* Save } 我的控制器: public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = “Modify this template to jump-start your ASP.NET MVC application.”; return View(); } public ActionResult About() { ViewBag.Message = “Your app description page.”; […]

MVC 4razor数据注释ReadOnly

ReadOnly属性似乎不在MVC 4中。可编辑(false)属性不能按照我希望的方式工作。 有类似的东西有效吗? 如果没有,那么我如何创建自己的ReadOnly属性,如下所示: public class aModel { [ReadOnly(true)] or just [ReadOnly] string aProperty {get; set;} } 所以我可以这样说: @Html.TextBoxFor(x=> x.aProperty) 而不是这(它确实有效): @Html.TextBoxFor(x=> x.aProperty , new { @readonly=”readonly”}) 或者这(它确实有效,但未提交值): @Html.TextBoxFor(x=> x.aProperty , new { disabled=”disabled”}) http://view.jquerymobile.com/1.3.2/dist/demos/widgets/forms/form-disabled.html 这样的事可能吗? https://stackoverflow.com/a/11702643/1339704 注意: [可编辑(假)]无效

MVC Razor需要获得Substring

我的观点中有以下内容 @Html.DisplayFor(modelItem => item.FirstName) 我需要获得名字的第一个首字母。 我试过了 @Html.DisplayFor(modelItem => item.FirstName).Substring(1,1) 但它似乎没有用。 我收到以下错误:..’System.Web.Mvc.MvcHtmlString’不包含’Substring’的定义,没有扩展名

在razor视图中使用Html.DisplayFor应用css类

在剃刀内部视图中,我使用模型来渲染标签 @Html.LabelFor(model => model.MyName, htmlAttributes: new { @class = “control-label col-md-6” }) 现在我想使用它的值而不是数据注释attr。 值,所以我尝试使用DisplayFor之类的 @Html.DisplayFor(model => model.MyName, new { @class = “control-label col-md-6” }) 此css类control-label col-md-6不适用。 为什么?