如何手动保存 Orchard.Forms 字段? [英] How can I manually save an Orchard.Forms field?

查看:47
本文介绍了如何手动保存 Orchard.Forms 字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过以下代码,我可以使用 Orchard.Forms 手动创建一个字段并将其显示在视图中.这是代码(仅相关):

MyLayoutForm.cs:

<代码>....公共无效描述(描述上下文上下文){Func<IShapeFactory, object>我的信息 =形状 =>{var f = Shape.Form(Id: "MyBasicInformation",基本信息:Shape.Fieldset(标题:T("基本信息"),名字:Shape.TextBox(ID:名字",名称:名字",标题:T(名字"),描述:T("该字段的名称"),要求:真)));返回 f;};context.Form("MyBasicInformation", myInformation);}....

MyContoller.cs:

<代码>....[主题]公共 ActionResult 基本信息(){var myBasicInformation = _formManager.Build("MyBasicInformation");//下面的 MyData 是我的视图模型var myData = new MyData { Form = myBasicInformation };返回视图(我的数据);}[主题][HttpPost, ActionName("基本信息")]public ActionResult BasicInformationPOST(string nextButton, FormCollection formCollection){_formManager.Validate(new ValidatingContext { FormName = "MyBasicInformation", ModelState = ModelState, ValueProvider = ValueProvider });if ((nextButton != null) && ModelState.IsValid){return RedirectToAction("个人信息");}var myBasicInformation = _formManager.Build("MyBasicInformation");_formManager.Bind(myBasicInformation, formCollection);//下面的 MyData 是我的视图模型var myData = new MyData { Form = myBasicInformation };返回视图(我的数据);}....

MyData.cs(视图模型):

公共类 MyData{公共动态表格{获取;放;}}

BasicInformation.cshtml:

<代码>....@using (Html.BeginFormAntiForgeryPost()){....@Display(Model.Form.BasicInformation.FirstName)<input type="submit" name="nextButton" value="Next"/>....}....

截至目前,名字文本框在我的视图中正确显示.我正在使用下一步"按钮转到下一个视图(稍后将是另一个问题).如果我单击下一步"并且文本框为空,则验证将触发(我省略了验证部分).如果我填写文本框并单击下一步",它会将我带到下一个视图 (PersonalInformation.cshtml).在第二个视图上单击返回"显然会显示一个空白的名字文本框,因为我还没有想出如何做到这一点.

我基本上已经使用 Projections/Rules/CustomForms 模块将代码拼凑在一起,以达到这一目的.但是,我已经在那里和其他地方尝试过示例来保存数据(实际上,可能使用 Session 来持久化它,但如果可能的话,这又是另一个关于如何做到这一点的问题 - 我知道其中的一些警告).此外,我上面列出的那些模块中的代码正在向管理屏幕中的特定模块(例如,投影)添加操作,因此该代码没有帮助(而且我无法完全弄清楚发生了什么).

那么,我该如何保存,例如名字"?

我创建了一个记录和一个简单的迁移.由于我只想存储非内容数据,因此我没有创建驱动程序或处理程序.但我不知道这是正确的方法,而且我不确定要在我的控制器中放入什么代码.

非常感谢任何示例.谢谢.

解决方案

正如我在评论中提到的,如果您需要在数据库中存储非内容数据,请按照我在评论中提供的答案进行操作,但存储起来要简单得多会话中的数据.尝试以下操作以访问和存储会话中的数据:

 private readonly IWorkContextAccessor _workContextAccessor;[主题][HttpPost, ActionName("基本信息")]public ActionResult BasicInformationPOST(string nextButton, FormCollection formCollection){...if ((nextButton != null) && ModelState.IsValid){var HttpContext = _workContextAccessor.GetContext().HttpContext;var basicInfo = new BasicInformation();TryUpdateModel(basicInfo);HttpContext.Session["BasicInfo"] = basicInfo;return RedirectToAction("个人信息");}...返回视图(我的数据);}

您可以稍后使用 HttpContext.Session["BasicInfo"] 在任何地方访问您的个人信息.另外一件事,不要忘记调用 Session.Abandon()完成用户注册后,请这样做:

//所有用户信息都被收集并存储在`database`中,所以让我们结束用户会话HttpContext.Session.Abandon();

编辑

Serializable 属性标记你的类(你计划在会话中疼痛),你可以阅读为什么你必须这样做 此处.

With the following code I am able to manually create a field using Orchard.Forms and display it in a view. Here is the code (relevant only):

MyLayoutForm.cs:

....
public void Describe(DescribeContext context) 
{
    Func<IShapeFactory, object> myInformation =
        shape => {
            var f = Shape.Form(
                Id: "MyBasicInformation",
                    BasicInformation: Shape.Fieldset(
                        Title: T("Basic Information"),
                        FirstName: Shape.TextBox(
                            Id: "FirstName", Name: "First Name",
                            Title: T("First Name"),
                            Description: T("The name for this field"),
            Required: true
                        )
                    )
                );
            return f;
        };
    context.Form("MyBasicInformation", myInformation);
}
....

MyContoller.cs:

....
[Themed]
public ActionResult BasicInformation()
{
    var myBasicInformation = _formManager.Build("MyBasicInformation");
    // MyData below is my View Model
    var myData = new MyData { Form = myBasicInformation };
    return View(myData);
}

[Themed]
[HttpPost, ActionName("BasicInformation")]
public ActionResult BasicInformationPOST(string nextButton, FormCollection formCollection)
{
    _formManager.Validate(new ValidatingContext { FormName = "MyBasicInformation", ModelState = ModelState, ValueProvider = ValueProvider });

    if ((nextButton != null) && ModelState.IsValid)
    {
        return RedirectToAction("PersonalInformation");
    }

    var myBasicInformation = _formManager.Build("MyBasicInformation");
    _formManager.Bind(myBasicInformation, formCollection);
    // MyData below is my View Model
    var myData = new MyData { Form = myBasicInformation };
    return View(myData);
}
....

MyData.cs (View Model):

public class MyData
{
    public dynamic Form { get; set; }
}

BasicInformation.cshtml:

....
@using (Html.BeginFormAntiForgeryPost())
{
    ....
    @Display(Model.Form.BasicInformation.FirstName)
    <input type="submit" name="nextButton" value="Next" />
    ....
}
....

As of right now the First Name textbox displays properly on my view. I am using a "Next" button to go to the next view (that will be another question later). Validation will fire (I left out the validation parts) if I click "Next" and the textbox is empty. If I fill in the textbox and click "Next" it takes me to the next view (PersonalInformation.cshtml). Clicking "Back" on the second view obviously shows a blank First Name textbox since I haven't figured out how to do that yet.

I've basically patched together code using the Projections/Rules/CustomForms modules to get this far. However, I have tried examples there and elsewhere to save the data (actually, to persist it maybe using Session, but again that's another question on how to do that if at all possible - I know some of the caveats therein). Additionally, the code in those modules I listed above are adding actions to the specific modules in the Admin screens (e.g., Projections), so that code isn't helpful (plus I can't totally figure out what's going on).

So, how can I go about saving, for example, "FirstName"?

I created a Record and a simple migration. Since I am just wanting to store non-content data I did not create a driver or handler. But I don't know that this is the right way to do it AND I am not sure what code to put in my controller.

Any examples are much appreciated. Thanks.

解决方案

As i mentioned in comment if you need to store a non content data in database then follow my answer provided in the comment, but it is much more simpler to store data in session.try following to access and store data in session:

 private readonly IWorkContextAccessor _workContextAccessor;



[Themed]
[HttpPost, ActionName("BasicInformation")]
public ActionResult BasicInformationPOST(string nextButton, FormCollection formCollection)
{
 .
 .
 .

if ((nextButton != null) && ModelState.IsValid)
{
    var HttpContext = _workContextAccessor.GetContext().HttpContext; 

    var basicInfo = new BasicInformation();

    TryUpdateModel(basicInfo);         

    HttpContext.Session["BasicInfo"] = basicInfo;

    return RedirectToAction("PersonalInformation");
}

.
.
.
return View(myData);
}

you can access your personal info anywhere later on with HttpContext.Session["BasicInfo"].one other thing , don't forget call Session.Abandon() after you finished registering user.do it so :

//all user information is gathered and stored in the `database` so let's end user session

   HttpContext.Session.Abandon();

EDIT

mark your Classes (which you have planed to sore in session) with Serializable attribute , you can read why you have to do so here.

这篇关于如何手动保存 Orchard.Forms 字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆