简体   繁体   English

奇怪的问题:在使用SQL Server 2008 R2的Razor在ASP.NET MVC 3中创建记录时,数据库更新率为60-40

[英]Weird problem: 60-40 database update rate while creating records in ASP.NET MVC 3 with Razor using SQL Server 2008 R2

Guys I have a really weird thing going on in mvc3. 伙计们,我在mvc3中发生了一件非常奇怪的事情。 Am using C# as the programming language and using SQL Server 2008 R2 as server and am using Linq-to-SQL. 我正在使用C#作为编程语言,并且正在使用SQL Server 2008 R2作为服务器,并且正在使用Linq-to-SQL。

When I am creating records things are being updated and now and then they don't get updated. 当我创建记录时,事物正在更新,现在又不更新。 I set breakpoints and checked and found nothing work in the code. 我设置了断点并检查,发现代码中没有任何工作。 But at times when doing breakpoint checks at times (not always) even though data is present in the page it (data) doesn't get passed to the UpdateModel(visitor) method and then I get the exception which says 但是有时有时(并非总是)执行断点检查,即使页面中存在数据,它(数据)也不会传递给UpdateModel(visitor)方法,然后我得到了异常提示

The model of type 'VisitorTrackingSystem.Models.Visitor' could not be updated. 无法更新“ VisitorTrackingSystem.Models.Visitor”类型的模型。

I am totally confused. 我很困惑。 Is there a bug in the MVC3 code that causes this? MVC3代码中是否存在导致此错误的错误? And I have a total of 7 tables in this database and only one particular table has the problem. 我在这个数据库中总共有7个表,只有一个特定的表有问题。

[HttpPost]
public ActionResult Create(FormCollection collection) //Creates a new record
{
    Visitor visitor = new Visitor();
    try
    {
        // TODO: Add insert logic here
        UpdateModel(visitor);
        visitorRepository.Add(visitor);
        visitorRepository.Save();                
    }
    catch(Exception e)
    {
        var exMsg = e.Message;
        return View("Exception");
    }  
    return RedirectToAction("Details", new { id = visitor.ID });
}

This exception could happen if the user tries to send some value which cannot be bound to the corresponding model property. 如果用户尝试发送一些无法绑定到相应模型属性的值,则可能发生此异常。 For example the user enters "foo bar" in a textbox which should be bound to an integer property. 例如,用户在应绑定到整数属性的文本框中输入"foo bar" You could use the TryUpdateModel instead: 您可以改用TryUpdateModel:

[HttpPost]
public ActionResult Create()
{
    Visitor visitor = new Visitor();
    if (!TryUpdateModel(visitor))
    {
        // There was a model error => redisplay the view so 
        // that the user can fix it
        return View();
    }

    try
    {
        visitorRepository.Add(visitor);
        visitorRepository.Save();                
    }
    catch(Exception e)
    {
        var exMsg = e.Message;
        return View("Exception");
    }  
    return RedirectToAction("Details", new { id = visitor.ID });
}

or a bit easier: 还是容易一些:

[HttpPost]
public ActionResult Create(Visitor visitor)
{
    if (!ModelState.IsValid)
    {
        // There was a model error => redisplay the view so 
        // that the user can fix it
        return View();
    }

    // you don't really need those try/catch here
    // simply leave the exception propagate and the 
    // HandleError global exception filter
    // will render the ~/Shared/Error.cshtml view passing
    // the exception details
    visitorRepository.Add(visitor);
    visitorRepository.Save();                
    return RedirectToAction("Details", new { id = visitor.ID });
}

I found what is causing this weirdness. 我发现了造成这种奇怪现象的原因。 Problem is on user end. 问题出在用户端。 When the value in a particular entry doesn't match with the respective foreign key the exception occurs. 当特定条目中的值与相应的外键不匹配时,将发生异常。 None the less the validation provided by Darin Dimitrov is useful. 尽管如此,Darin Dimitrov提供的验证仍然有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SQL Server 2008 R2在ASP.NET C#中创建Crystal Report - Creating Crystal Report in asp.net c# with SQL Server 2008 R2 部署使用Asp.Net,C#和Sql Server 2008 R2构建的Web应用程序 - Deploying an Web Application built using Asp.Net,C# and Sql Server 2008 R2 使用SqlParameter实现C#ASP.NET和SQL Server 2008 R2的安全性 - Using SqlParameter for security with C# ASP.NET and SQL Server 2008 R2 存储过程不返回任何数据asp.net sql server 2008 r2 - store procedure don't return any data asp.net sql server 2008 r2 C#ASP.NET无法将联系页面上传到SQL Server 2008 R2 - C# ASP.NET trouble uploading contact page to SQL Server 2008 R2 asp.net中Windows Server 2008 R2上的PDF缩略图 - PDF Thumbnails on windows server 2008 R2 in asp.net 通过网络访问SQL Server数据库2008 R2时出现问题 - Problem accessing sql server database 2008 r2 over network 30秒后在ASP.NET上超时,在SQL Server 2008 R2上没有超时 - Timeout on ASP.NET after 30 seconds, on SQL server 2008 R2 no timeout 如何通过asp.net更新SQL Server数据库中的记录? - How to update records in SQL Server database through asp.net? 具有SQL 2008 R2开发人员和SQL Express 2008 R2的ASP.Net C#ASPNETDB.mdb - ASP.Net C# ASPNETDB.mdb with SQL 2008 R2 Developers and SQL Express 2008 R2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM