Sitecore WFFM - How to trigger a form externally - c#

I have a web forms for marketers, and I need to "run" that webform through code from somewhere else.
Is there a way to load the webform and run its execute action? For example with a regular sitecore item I can get it based on its ID, but I don't seem to be able to find a way to cast a regular sitecore item, to a WFFM item and then execute its save actions (and possibly set its fields).

It sounds like you are trying to insert data into the WFFM database without having to run through the WFFM submit action? Did I get that right?
If so, you could submit form code to the WFFM DB using the following code, and it would show up seamlessly in the report page of that form:
Say you have your list of fields populated in a list of this class:
public class WffmField
{
public string FieldName { get; set; }
public string FieldGuid { get; set; }
public string FieldValue { get; set; }
}
the field guid would be the guid from sitecore:
You can then save to the WFFM database:
// This should be populated with the data you want to send to the WFFM database
var fields = new List<WffmField>();
var wffmDatabaseFields = fields.Select(GetWFFMDatabaseField).ToList();
Sitecore.Forms.Data.DataManager.InsertForm(
formId: new Sitecore.Data.ID("<Form guid here>"),
fields: new AdaptedResultList(wffmDatabaseFields),
sessionID: AnalyticsTracker.SessionId,
data: null);
Hope this helps!

Related

C# MVC: Can we have partial update on the model?

We have c# model class with following fields:
public class BasicDetailsModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
}
Once user clicks on save button with (Name="x", Email="x#gmail.com", Mobile=989), these details get saved in database. Now, in case user updates any of these fields i.e. Name to "y", we have to send Email,Mobile too although these have not changed.
I have oversimplified the problem here. We have multiple cases where Models have over 20 fields and we are updating more than 5 models through single save click.
How can we update only what's needed and assure everything else already saved remain as it is i.e. in this case client would only send (Name="y") and Database will have (Name="y", Email="x#gmail.com", Mobile=989).
I don't want to make multiple database calls before saving and want flexibility that user remove value from already entered fields i.e. can change Name from "y" to null.

Asp.net MVC 3 - Data Validation

I'm new at ASP.NET MVC web framework. My database is compound of a single model class("Movie"). I need to validate user's from entering existing data, for instance, a database row could be:
Title - "Indiana Jones and the lost Arc"
Price - $10.00
If another user tries to insert into the database the same data above, provide an error message and prevent from submitting the form collection.
First of all, I think that your question need a "program as answer" but I'll try to suggest you from where to start for working with validation. Suppose you have this model:
class Movie {
public Guid Id { get; set; }
[Required(ErrorMessage="Title is required.")]
[Remote("UniqueTitle", "Validation")]
public String Title { get; set; }
[Required(ErrorMessage="Price is required.")]
public float Price { get; set; }
}
You can decorate it for "simple validation" using Data Annotation. I've used a specific attribute, called Remote.
This attribute allow you to define a custom, server-side, logic to validate the model.
Now, you can create a validation controller where check that provided value is not already in use:
class ValidationController : Controller {
private IDbContext db = ...;
public ActionResult UniqueTitle(String title) {
var item = db.Movies.FirstOrDefault(m => m.Title.Equals(title));
return Json(item == null, JsonRequestBehavior.AllowGet);
}
}
Now you are ready to validate your model.
I hope this can help.

Pass MVC Controller a list of columns and return data for those columns?

I am trying to build a helper class for Datatables and need some advice.
I want to create a helper that I can pass in an object that represents the settings for a DataTables that is typed by a specific model. This object would contain a collection of expressions that represent the columns to be used by the helper. As part of the DataTables object there will be a property that holds the URL that servers up the data for the grid. This will effectively be the sAjaxSource parameter. As part of the URL call, a JSON package will be sent that will contains the information about the columns. This whole section, I understand how to build. Basically, I would build out the DataTables JavaScript code including column definitions and create a JSON object that represents the columns I want to pass in the URL.
The area I needs some advice on is building the data for the specific columns once hte call is made to the server / controller. So my controller may look like this:
public ActionResult GetUsersList(IEnumerable<DatatableColumnJson> columns)
{
var users = _someUserRepository.GetAll();
foreach (var user in users)
{
//Here I would build the JSON that DataTables needs to render the grid.
//I would iterate through the users and pull out the data for the specific columns
}
return Json(someCompleteDataTablesObject, JsonRequestBehavior.AllowGet);
}
Supporting Classes:
public class DatatableColumnJson
{
public string Name { get; set; }
public string Visible { get; set; }
}
public class User
{
public string UserName { get; set; }
public string Email { get; set; }
}
So I first thought I could use reflection, but that seems to be a little intensive, maybe. Another thought was to transform the column names into expressions to possibly pull out the data. I want something efficient and that doesn't have a lot of overhead, but maybe I am asking too much.

ASP.NET MVC3 Intranet Site

I'm struggling with the design aspect of building this web site, its my first website and I'm not sure of the correct direction i need to take this project in. I've posted on this previously but not done a good job of explaining it. So I'll attempt to do so now.
The Site will be used for submitting "Innovation Ideas" from employees. It will have to connect up to an already existing MS Access Database. There will be two tables in this database that it has to communicate with.
The first one Is the InnovationSubmission Table which which looks similar to this :-
ID(Auto Generated),
Short Description,
Long Description,
Name,
Email Address,
Date(From Date.Time.Now()) - on Submission.
Team Name
Area (Area of Business)
The User will use a Web Form(View) to enter the details above, it will be validated, then saved to the back end database. I have this working in a fashion. The issue has started when I have tried to introduce two DropDownlistsFor contorls based on another table which is below :-
AREA A - Team1, Team3, Team5, Team7
AREA B - Team2, Team4, Team6, Team8
This is just sample Data, there are only two different areas in the table, but there will be over 50 teams split across them. I will be looking to have the Teams dropdownList filter on the value in the Area DropDownlist.
In my Models folder I have created a InnovationSubmission Class, that replicates the table in the database, this class is used as a strongly typed data type in the View representing the Submission Form. Its how i Validate the User input and I pass this class to a c# method that sends the data back using ADO.NET.
I'm struggling with how I should be trying to implement the dropdownlists.
Will I need to create a class similar to the InnovationSubmission Class, to represent the Teams/ Area Table?
Is the table at present structured in the best way for this project?
Can I populate both dropdownlists from the 1 table?
How do I relate the Teams & Area Columns?
Any Help would be greatly appreciated!?!
Would this be the correct way to design my View Model :-
public class MyViewModel
{
public int ID { get; set; }
public string shortDesc { get; set; }
public string longDesc { get; set; }
public string Status { get; set; }
public string originator { get; set; }
public string originatorEmail { get; set; }
public IEnumerable<Area> area { get; set; }
public IEnumerable<Team> team { get; set; }
}
public class Team
{
public string teamName { get; set; }
}
public class Area
{
public string area { get; set; }
}
You seem to be talking about cascading dropdown lists where the values of the second one update based on the selection made in the first one. I have illustrated an example of how this could be achieved in this post. The idea is that you subscribe to the .change event of the first dropdownlist and then trigger an AJAX request to a controller action passing the selected value in which you wold query the database and return a list of possible options.

Editing form fields via ajax and .net mvc

I have a number of inputs on my page. I would like to save the changes to the model on the input blur, so as I change the value of each input it gets saved back to server, like Google contacts.
<input id="FirstName" name="FirstName">Jack</input>
I create a blur event using jquery to post the value back to the server. It posts a structure with the name of the input, the value and an id of the entity.
$.post(url, { id: "2", key: "FirstName", value: "Jack" }, successFuction);
In my controller I have:
public ActionResult EditField(int id, string key, string value)
I then retrieve the entity using EntityFramework with the id. I then wanted to update the property on the model for the field.
var entity = _db.Get(id);
entity[key] = value;
return Content "Success";
Which I obviously can't do! The only way I can think off is multiple methods for each field so EditName, EditAddress etc. which seems wrong. I want this method to be able to handle each property of the model.
What is a better way to structure the controller instead of writing multiple methods for each individual field?
You could post your entire form (e.g. first name, last name, etc.) on each blur for any of your fields (this should be fine since you're saving all changes as the user progresses on the form anyway). Unless you're really trying to save bytes, posting the whole form seems fine.
You could just post the field name and then use reflection to look up the property of your object and set the value.
I think that you can do it if you are willing to model the entity in a general way:
public class FieldEntity {
public int Id { get; set; }
public string Key { get; set; }
public string Value { get; set; }
}
Then use it inside the context like:
var fieldEntity = db.Find(id);
fieldEntity.Key = key;
fieldEntity.Value = value;
db.SaveChanges();
However, it is usually better to structure data in a way that is meaningful. In the example you describe it looks like you might have a Person and Address entity. So why not have a Person entity that has a property Address?

Categories

Resources