【问题标题】:Using the Trim function in ASP.net MVC 3在 ASP.net MVC 3 中使用 Trim 函数
【发布时间】:2012-03-29 14:19:01
【问题描述】:

大家好,MVC 新手,我需要从我的数据中删除一些 %20,以便正确显示。目前,我的 CRUD 正确显示索引并且它能够编辑用户创建的记录,但是当我尝试编辑来自旧数据库的记录时,它不起作用并引发无法找到资源的错误。我仔细查看了旧数据库记录的 URL,发现大多数数据库记录是 %20%20%20%20%20。这告诉我数据库记录后面有额外的空格,因为这就是 %20 的含义。我做了一些研究,发现 Trim 功能可以为我去除多余的字符。我只是不明白如何在我的具体情况下使用修剪功能。

我认为我需要在我的 Edit ActionResult 下的 Controller 中使用该功能,但是当我在几个不同的地方尝试它时,我没有运气。我以为我理解了这个函数是如何工作的,但结果没有改变,因为我被卡住了。我希望这能很好地解释这个问题,下面我已经发布了我的控制器的代码,其中包含我的 CRUD 方法,但是如果您需要我发布任何其他类,请告诉我。

您可能会注意到我在编辑方法中留下了一个修剪方法,这是我解决问题的尝试,如果我完全错了可以很容易地删除,因为它不起作用,我相信我是!感谢您的帮助,如果您需要我的编辑课程,请询问我会经常回来查看!对于阅读本文以帮助他们解决相同问题的任何人,我一定会在解决后发布解决方案!

PaController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DBFirstMVC.Models;
using System.Data;

namespace DBFirstMVC.Controllers
{
    public class PaController : Controller
    {
        PaEntities db = new PaEntities();
        //
        // GET: /Pa/

        public ActionResult Index()
        {
            using (var db = new PaEntities())
            {
                return View(db.iamp_mapping.ToList());
            }
        }

        //
        // GET: /Pa/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Pa/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Pa/Create

        [HttpPost]
        public ActionResult Create(iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.iamp_mapping.Add(IAMP);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Pa/Edit/5

        public ActionResult Edit(string id)
        {

            using (var db = new PaEntities())

            {
                string trimmedID = id.Trim();
                return View(db.iamp_mapping.Find(trimmedID));
            }
        }

        //
        // POST: /Pa/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.Entry(IAMP).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("");
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Pa/Delete/5

        public ActionResult Delete(string id)
        {
            using (var db = new PaEntities())
            {

                return View(db.iamp_mapping.Find(id));
            }
        }

        //
        // POST: /Pa/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.Entry(IAMP).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("");
                }

            }
            catch
            {
                return View();
            }
        }
    }
}

【问题讨论】:

    标签: asp.net-mvc-3 whitespace edit trim


    【解决方案1】:

    您正确使用了修剪功能,因此您的问题似乎出在其他地方。也就是说,我建议您在显示索引页面之前进行修剪,否则您将不得不在将 id 作为参数的任何操作(编辑、删除等)中修剪 id。

    【讨论】:

    • 我修复了它我在 MS SQL 中而不是在 MVC 中进行了 LTrim RTrim,它在问题到达 MVC 之前解决了问题,感谢您的帮助!
    • @Goldentp 你能解释一下去LTrim RTrim in MS SQL instead of in the MVC 的方法吗?我也有这个问题。
    【解决方案2】:

    听起来你想要 HttpUtility.HtmlDecode:

    http://msdn.microsoft.com/en-us/library/7c5fyk1k.aspx

    【讨论】:

      猜你喜欢
      • 2011-09-19
      • 1970-01-01
      • 2023-01-15
      • 1970-01-01
      • 2011-09-02
      • 2013-01-05
      • 1970-01-01
      • 2012-04-12
      • 2012-08-10
      相关资源
      最近更新 更多