简体   繁体   English

序列化模型数据

[英]Serializing Model Data

EDIT 编辑

I am working on a project that will try to pre-approve customers for a credit card. 我正在做一个项目,该项目将尝试预先批准客户的信用卡。 This application 这个应用程序

  1. Be called from a different application passing information about the customer 从另一个应用程序调用,传递有关客户的信息
  2. Consume a wcf service that will do various things including calling a third party web service for the actual credit approval 使用wcf服务,它将执行各种操作,包括调用第三方Web服务进行实际的信用批准

This new system is being developed using ASP.NET MVC, to which I am extremely new. 这个新系统是使用ASP.NET MVC开发的,对于我来说,这是非常新的。

So, I have the initial piece working, I can pass specific data to this mvc application and display the credit results to a view. 因此,我已经开始工作,可以将特定数据传递到此mvc应用程序,并将功劳结果显示在视图中。

At this point, if the customer is approved, the user either needs to indicate if the customer wants to accept the offer or not. 此时,如果客户被批准,则用户要么需要表明客户是否要接受要约。 If they do, then the user needs to indicate which one,of four, credit card products the customer wants. 如果他们这样做了,那么用户需要指出客户想要四种信用卡产品中的哪一种。 If they want to generate an ACH payment, etc. If the customer declines the offer, the user has to select a reason for the decline. 如果他们想产生ACH付款等。如果客户拒绝报价,则用户必须选择拒绝的原因。

OK, so all of that is working as well. 好的,所有这些都可以正常工作。

My problem lies in what to do if validation fails. 我的问题在于验证失败时该怎么办。 I have specified several required if attributes for conditional validation. 我为条件验证指定了一些必需的if属性。 If the user forgets to select a credit card product, that would fail conditional validation if the customer accepted the offer. 如果用户忘记选择信用卡产品,那么如果客户接受了要约,则将无法通过条件验证。 In these rare, yet conceivable, scenarios, I need to send the credit results, which include the card products offered, the decline reasons, customer name, etc. 在这些罕见但可以想象的情况下,我需要发送信用结果,其中包括所提供的卡产品,拒绝原因,客户姓名等。

So what I really need to understand is how can I send that credit response data back up to the controller I am using for the decision information. 因此,我真正需要了解的是如何将信用响应数据发送回用于决策信息的控制器。

The initial controller is simply called Index 初始控制器简称为Index

[HttpPost]
public ViewResult Index ( PrescreenCCRequest request )
{
    WCFClient client = new WCFClient ( );

    PrescreenCCResult result = client.ProcessPrescreen ( request );

    return View ( result );
}

within the view for this controller I define a form for the various user options 在此控制器的视图中,我定义了各种用户选项的表单

@using ( Html.BeginForm ( "Save", "Prescreen", FormMethod.Post ) )
{
    ...Control definitions left out...
}

finally, the controller for the "Save" action: 最后,控制器执行“保存”操作:

[HttpPost]
public ActionResult Save ( PrescreenModel formData )
{
    if ( !ModelState.IsValid )
    {
        return View ( "~/Views/Prescreen/Index.cshtml", formData );
    }

    ... Additional Code left out ...
    return View ( );
}

So that is what I am working towards 这就是我正在努力的方向

MVC Dll: MVC Dll:

If you cannot find the dll when using the reference manager it sounds like MVC might not have installed correctly as they should be included in the Framework references. 如果在使用引用管理器时找不到dll,则听起来MVC可能未正确安装,因为它们应该包含在Framework引用中。 Here is what shows in my VS RM 这是我的VS RM中显示的内容

I would suggest you look at installing MVC using the nuget package manager that is built into Visual Studio 我建议您考虑使用Visual Studio内置的nuget软件包管理器安装MVC。

在此处输入图片说明

Persisting Data Across Controllers: 跨控制器持久化数据:

This is a topic on its own and has various approaches. 这本身就是一个主题,并且有多种方法。 It depends on many factors like how long you would like the data to be persisted etc. 这取决于许多因素,例如您希望将数据保留多长时间等。

If there data is persistence is fairly short lived I would use the Session object or the TempData object when accessing it across requests. 如果存在数据的持久性很短,那么在跨请求访问它时,我将使用Session对象或TempData对象。

Serializing Data: 序列化数据:

What format is the data? 数据是什么格式? If it is in Json I would recommend using the NewtonSoft as this has a very flexible object serializer/deserializer that can be used to validate object data into custom data models. 如果在Json中,我建议使用NewtonSoft,因为它具有非常灵活的对象序列化器/反序列化器,可用于将对象数据验证为自定义数据模型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM