问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/17429967/how-can-i-manually-save-an-orchard-forms-field