asp.net-mvc-3 相关问题

ASP.NET MVC 3是用于在.NET框架中开发Web应用程序的Model-View-Controller扩展的第三个主要版本。

如何编写 DbContext 类。

如果我有一个名为 STOREitems 的数据库,并且该数据库中有一个表标题为 PRICES,模型名称为 Price,我会这样写吗? 公共类 STOREitemsDBContext :DbC...

回答 2 投票 0

如何在 Razor 视图中设置日期时间格式?

我有这样的变量: 日期时间 crd = a.CreationDate; //(在 C# 中显示为变量,但在 razor 视图中可用) 我想使用以下格式将这些显示为日期:11/06/2011 02:11 理想情况下我会...

回答 7 投票 0

手动调用 ModelState 验证

我正在使用 ASP.NET MVC 3 代码优先,并且我已将验证数据注释添加到我的模型中。这是一个示例模型: 公开课产品 { 公共 int ProductId { 获取;放; } [必填(

回答 5 投票 0

在 Razor 中连接字符串

如何使用 Razor 语法连接两个字符串? 如果我有:@Model.address 和@Model.city,并且我希望输出为地址城市,我会做什么?就像@Model.address + " " + ...那么简单吗

回答 6 投票 0

在 ASP.NET MVC Razor 视图中将换行符替换为 <br />

我有一个接受输入的文本区域控件。我稍后尝试通过简单地使用以下命令将该文本渲染到视图中: @Model.CommentText 这正确地编码了任何值。不过,我想更换...

回答 8 投票 0

单击链接时将值传递到编辑器

我创建了一个页面,其中包含以下链接,其值在后面。 例如: 示例链接:654465465! 当用户点击链接时,URL 将被获取到 http://www.example.com/6544654...

回答 1 投票 0

MVC 3 Razor webgrid - 如何更改表格宽度

我有一个问题,无论我尝试什么,我都无法更改网络网格表格的宽度。它忽略其父 div 的 css,并且使用 css 更改网格的宽度没有任何效果。 韦伯...

回答 2 投票 0

在 Model mvc3 中渲染列表的一部分

我列出了问题(模型中约有 1000 个问题)。 我想在用户向下滚动将呈现接下来的 100 个问题后首先呈现 100 个问题。 你知道怎么做吗?

回答 2 投票 0

MVC 表单加载调用控制器方法及其重载方法而不是调用单击方法?附上代码

我正在单击按钮保存数据,但在第一次加载进入重载方法时查看? 我的视图代码是这样的, @using (Html.BeginForm("ManageQuestion", "Questions", FormMethod.Post)) { 我正在单击按钮保存数据,但在第一次加载进入重载方法时查看? 我的视图代码就像, @using (Html.BeginForm("ManageQuestion", "Questions", FormMethod.Post)) { <input type="submit" value="Save" /> } 我的控制器就像, public ActionResult ManageQuestion() { //List<SelectListItem> QuestionType = Survey(); //return View(QuestionType); return View(); } [HttpPost] public ActionResult ManageQuestion(Question Objquest) { if (ModelState.IsValid) { SurveyAppEntities ObjEntity = new SurveyAppEntities(); string strDDLValue = Request.Form["DDlDemo"].ToString(); Objquest.QuestionType = strDDLValue; ObjEntity.Questions.Add(Objquest); ObjEntity.SaveChanges(); ViewData["error"] = "Question Saved successfully"; if (Objquest.ID > 0) { // ViewBag.Success = "Inserted"; } ModelState.Clear(); } return View(); } } 我认为它必须在单击按钮时调用重载 ManageQuestion 方法,但是当第一次加载视图时,它会进入重载方法,从而导致错误。 我从网上得到了一个具有相同场景的示例,但重载方法在第一个表单加载时没有调用? 希望您的建议 谢谢 您似乎想避免在首次加载视图时执行 [HttpPost] 方法。实现此目的的一种常见方法是检查请求是否是 POST 请求。您可以修改 [HttpPost] 方法,使其仅在 POST 请求时执行逻辑。这是一个例子: public ActionResult ManageQuestion() { // This method will be called when the view is first loaded // Add any necessary logic here return View(); } [HttpPost] public ActionResult ManageQuestion(Question Objquest) { // This method will be called when the form is submitted (POST request) if (ModelState.IsValid) { // Your logic for saving the data } // Regardless of whether the data is saved or not, return to the view return View(); } 通过检查 HttpContext.Request.HttpMethod 或使用 HttpPost 属性,可以确保 [HttpPost] 方法仅在提交表单时执行,而不是在视图初始加载时执行。在上面的例子中,[HttpPost]方法内部的逻辑只有在请求是POST请求时才会被执行。 记得将[HttpPost]方法中的逻辑注释替换为你实际的数据保存逻辑。

回答 1 投票 0

如何将上传的文件从javascript发送到MVC中的控制器?

在我的 MVC 中,我有一个视图,其中包含一个文件上传控件和一个按钮。 在我的 MVC 中,我有一个视图,其中包含一个文件上传控件和一个按钮。 <input type="file" id="Uploadfile" /> <input type="button" onclick()="GetFile();/> Javascript函数如下 function GetFile() { var file_data = $("#Uploadfile").prop("files")[0]; window.location.href="Calculation/Final?files="+file_data; } 我需要通过文件上传控件将选定的文件传递/发送到mvc中的控制器。 我有控制器 public ActionResult Final(HttpPostedFileBase files) { // here I have got the files value is null. } 如何获取选中的文件并发送给控制器? 我在我的项目中提供了类似的功能。 工作代码看起来像这样: 控制器类 [HttpPost] public ActionResult UploadFile(YourModel model1) { foreach (string file in Request.Files) { HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase; if (hpf.ContentLength > 0) { string folderPath = Server.MapPath("~/ServerFolderPath"); Directory.CreateDirectory(folderPath); string savedFileName = Server.MapPath("~/ServerFolderPath/" + hpf.FileName); hpf.SaveAs(savedFileName); return Content("File Uploaded Successfully"); } else { return Content("Invalid File"); } model1.Image = "~/ServerFolderPath/" + hpf.FileName; } //Refactor the code as per your need return View(); } 查看 @using (@Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" })) { <table style="border: solid thin; margin: 10px 10px 10px 10px"> <tr style="margin-top: 10px"> <td> @Html.Label("Select a File to Upload") <br /> <br /> <input type="file" name="myfile"> <input type="submit" value="Upload" /> </td> </tr> </table> } 您无法通过 javascript 发送文件内容(除非 HTMl5)。而你做的完全错误。如果您想通过 FileReader api 执行基于 HTML5 的解决方案,那么您需要检查一下。 文件读取器 API 只需放置一个表单标签并在控制器操作中使用与输入相同的名称即可执行模型绑定 @using(Html.BeginForm("yourAction","YourControl",FormMethod.Post)) { <input type="file" id="fileUpload" /> } 然后在控制器中。 [HTTPPost] public ActionResult Final(HttpPostedFileBase fileUpload) { //here i have got the files value is null. } 下面的代码将以隐藏形式进行完整的回发,这将给人一种ajax文件上传的错觉。尝试一下: 更新: JS function Upload(sender) { var iframe = $("<iframe>").hide(); var newForm = $("<FORM>"); newForm.attr({ method: "POST", enctype: "multipart/form-data", action: "/ControllerName/Final" }); var $this = $(sender), $clone = $this.clone(); $this.after($clone).appendTo($(newForm)); iframe.appendTo($("html")).contents().find('body').html($(newForm)); newForm.submit(); } HTML <input type="file" id="Uploadfile" name="Uploadfile" /> <input type="button" onclick="Upload($('#UploadFile'));"/> 控制器 public ActionResult Final(HttpPostedFileBase Uploadfile) { //here you can use uploaded file } 作为 Ravi 回答的补充,我建议使用以下 using 声明: @using(Html.BeginForm("yourAction","YourControl",FormMethod.Post, new { enctype="multipart/form-data" })) { <input type="file" id="fileUpload" /> } 可以使用json数据查看。 举个例子, 控制器 public ActionResult Products(string categoryid) { List<catProducts> lst = bindProducts(categoryid); return View(lst); } public JsonResult Productsview(string categoryid) { //write your logic var Data = new { ok = true, catid = categoryid}; return Json(Data, JsonRequestBehavior.AllowGet); } 查看: @{ ViewBag.Title = "Index"; } @model ASP.NETMVC.Controllers.Categories <h2>List Of Categories</h2> @Html.ListBox("lst_categories", (IEnumerable<SelectListItem>) ViewBag.Categories) <script type="text/javascript"> $(function () { $('#lst_categories').change(function () { var catid = $('#lst_categories :selected').val(); $.ajax({ url: '@Url.Action("Productsview", "Jquery")', type: 'GET', dataType: 'json', data: { categoryid: catid }, cache: false, success: function (Data) { if (Data.ok) { var link = "@Url.Action("Products", "Jquery", new { categoryid = "catid" })"; link = link.replace("catid", Data.catid); alert(link); window.location.href = link; } } }); }); }); </script>

回答 5 投票 0

如何使用数据库在 Asp.net MVC 中实现博客网站的查看计数功能?

我在 Asp.net 中使用 MVC 模式开发了一个博客网站,并且我希望使用数据库为我的博客实现视图计数功能。具体来说,我想显示用户数量...

回答 1 投票 0

无法使用chocolatey安装ASP.NET MVC 3

过去,我使用以下命令在 dockerfile 中使用 Chocolaty 安装 MVC 3,没有出现问题。由于某种原因,我开始收到 404 not found 消息。我觉得包它...

回答 1 投票 0

ModelState 对于空的非必填字段被标记为无效

有点神秘。我有一个带有 Year 属性的视图模型: 公共类 TradeSpendingSalesViewModel { 公共字符串产品代码{获取;放; } 公共 IEnumerable 有点神秘。我有一个带有 Year 属性的视图模型: public class TradeSpendingSalesViewModel { public string ProductCode { get; set; } public IEnumerable<SelectListItem> AllowTypeSelect { get; set; } public string AllowType { get; set; } public IEnumerable<SelectListItem> YearsSelect { get; set; } public int Year { get; set; } } 如果我将空视图模型发布到我的控制器: [HttpPost] public ActionResult Index(TradeSpendingSalesViewModel vm) { var allErrors = ModelState.Values.SelectMany(v => v.Errors); foreach (var e in allErrors) { Response.Write(e.ErrorMessage); } } 然后我收到一个错误,并显示以下消息:“年份字段是必需的。” 由于我没有使用 Required 属性注释 viewmodel Year 字段,所以我不清楚为什么会生成此错误。 有什么想法吗? ValueTypes 默认情况下在 mvc 中隐式标记为 Required。实际上,这样做是有目的的,因为根据定义它们是不可为空的。 我建议你将 Year 设置为 int?,否则,如果你的情况不正确,你可以改为 false DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes 在 Global.asax.cs 中。 我的第一个猜测是它抛出了一个异常,您没有设置年份并且它为空?如果您将年份设置为 Nullable 它不会抛出所需的消息吗? 我仍然没想到会需要它,这是在黑暗中拍摄 这仍然可能是 .NET Core 项目中的一个问题。我在这里找到了解决方案: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-8.0#non-nullable-reference-types-and-required-attribute .NET 会将您的参数标记为无效,如果它不是所需的可空类型。您可以在 MvcOptions 中将 SuppressImplicitRequiredAttributeForNonNullableReferenceTypes 属性设置为 true。 Program.cs 中的示例: services.AddControllers(option => { option.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true; }); 当您设置此选项时。它只会检查所需的属性。或者你可以使用“?”标记以使您的属性可为空,这也可以。

回答 3 投票 0

MVC 如何忽略嵌套视图模型的验证

我有一个页面,我将两个视图模型发布到控制器,查询和预约。预约嵌套在查询中。用户可以选择向我们提交查询,而无需创建

回答 5 投票 0

jQueryUI 弹出窗口无法多次工作

我正在从事mvc项目,其中大量使用了jQuery。在其中一个视图中,我们使用手风琴控件,内部有多个(三个)视图。 jQuery 弹出窗口在第一个面板中工作正常,但是一旦我

回答 1 投票 0

输出nbsp;在 Razor 中通过变量?

我这里有一个空白。 我需要输出“nbsp;”通过变量: 字符串 strSpacer = "  "; 所以正在尝试: @:strSpacer 不起作用,尽管我确实在...

回答 2 投票 0

MVC 3 控制器上的单元测试返回 null 结果,但我可以在立即窗口中看到它们

我是 MVC 和单元测试的新手。我正在使用 Visual Studio 单元测试框架来测试产品控制器。控制器在实际网站上运行,但在单元测试中始终返回 null。我有开关

回答 3 投票 0

POST json 字典

我正在尝试以下操作:内部带有字典的模型在第一个ajax请求上发送它,然后将结果再次序列化并将其发送回控制器。 这应该测试我是否...

回答 11 投票 0

为什么处理图像时 ModelState 为 false

公共 ActionResult 注册(UserTable u) { 字符串文件名 = Path.GetFileNameWithoutExtension(u.ImageFile.FileName); 字符串扩展名 = Path.GetExtension(u.ImageFile.FileName); 文件N...

回答 1 投票 0

部署asp.net,IIS没有导入应用程序选项

我正在尝试根据此站点 http://www.shubho.net/2011/01/quick-deployment-of-aspnet-applications 中的步骤使用 IIS 在本地部署 ASP.net 网页。 html 然而,当我尝试...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.