Get a reference to current Listitem from within custom SPField - sharepoint-2007

I've successfully created a custom field inheriting from SPFieldText and happily have complete control over rendering it on the input form as a control.
Problem:
I have a need to create a link to a popup with the ListId and ListitemID in the querystring when rendering the field using GetFieldValueAsHtml().
Something like this:
public class CustomField : SPFieldText
{
public CustomField (SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public CustomField (SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{
}
public override string GetFieldValueAsHtml(object value)
{
return string.Format(
"javascript:window.open('{0}/_layouts/Popup.aspx?ListId={1}&ItemId={2}','Popup','status=0,scrollbars=0,titlebar=0,resizable=1,toolbar=0,location=0,width=600,height=500');return false;",
SPContext.Current.Web.ServerRelativeUrl.TrimEnd('/'),
LISTID, LISTITEM.ID
);
}
Clearly SPContext doesn't hold a reference to the list or item and none of the properties seem to expose the current item. I tried overloading properties in the control but these don't seem to be invoked when rendering the field.
// None of these properties are invoked when rendering the field as above
public class CustomFieldControl : TextField
{
public override object ItemFieldValue
public override object ListItemFieldValue
public override string Text
public override object Value
}
I've experimented with the RenderPattern in fldtypes_Custom.xml but again this is also ignored when rendering the field using GetFieldValueAsHtml();
Am I naively expecting something that's not possible?
I'm open to any approach that avoids rewriting the web part... or just tell me it can't be done.
(The existing web part renders a grid and calls GetFieldValueAsHtml(). We know we can change the web part to achieve this but that's not an ideal solution for other reasons).

Not sure if this will work with SharePoint 2007, but with SharePoint 2010 one can easily fetch the currently being displayed ListItem by using SPContext.Current.ListItem.

For anyone stumbling across this, I confirmed that what I was aiming to do is not possible.
We were forced to make changes in the web part to achieve this level of customization. As outlined in the question, The existing web part renders a grid and calls GetFieldValueAsHtml().

Related

MVC - Inherited view model members [duplicate]

My Post call does not return the correct Model type. It always use the baseObject instead of the correct derived object that I passed in from the Get
RestaurantViewModel.cs
public class RestaurantViewModel{
public Food BaseFoodObject{get;set;}
}
Food.cs
public class Food{
public string Price{get;set;)
}
Bread.cs -- Inherit from Food
public class Bread:Food{
public int Unit{get;set;}
}
Milk.cs -- Inherit from Food
public class Milk:Food{
public string Brand{get;set}
}
Editor For Template for Bread. Display the unit and allow user to edit
Index.html
#Model RestaurantViewModel
#using(Html.BeginForm("SaveFood", "Food"))
{
#Html.EditorFor(m=>m.BaseFoodObject)
<input type="submit" value="Process"/>
}
Bread.cshtml
#Model Bread
<div>
#Html.TextboxFor(bread=>bread.Unit)
</div>
FoodController.cs
public ActionResult Index(){
Bread bread = new Bread(){
Price = "$10",
Unit = 1
}
RestaurantViewModel viewModel = new RestaurantViewModel(){
BaseFoodObject = bread
}
return View(viewModel);
}
public ActionResult Post(RestaurantViewModel viewModelPost)
{
// When I inspect the viewModelPost, there is no attribute for unit
}
Final Result:
1. The display looks correct. EditorFor is smart enough to pick the correct editor template and display the value correctly
2. The Save does not work. The Unit attribute of Bread Object does not get passed in with the RestaurantViewModel. The reason for that is the RestaurantViewModel used the Food object instead of Bread
I hope there is away to modify the EditorFor and tell it to use the Model in the View or the Object Type that I passed in when I display it.
Thanks
Update 1: I solved this problem by using the custom binder and using a factory to decide which object I really want. This helps construct the correct Model which I want
MVC is stateless. A couple of references.
There's a couple of statements in your question that conflict with this, and how MVC binding works eg:
My Post call does not return the correct Model type.
Possibly just terminology, but your Post call does not 'return a model type' - it goes into the model that's defined in the post action, in this case RestaurantViewModel.
instead of the correct derived object that I passed in from the Get
because it is stateless, it knows nothing about the model you passed in from the get... absolutely nothing.
The final html rendered via the getaction+view.cshtml+model is not linked to the postaction. You could just as easily take the rendered html, save it, reboot your PC, reload the rendered html and it will work exactly the same way.
a way to modify the EditorFor and tell it to use the Model in the View or the Object Type that I passed in when I display it
When you use EditorFor it sets an ID and name attribute based on the model it was bound to, so it already does this, but perhaps you are not binding to the model you want to bind to to get the correct id.
So, to the question, if, in 'normal' C# code you were to instantiate a new instance of RestaurantViewModel, what would you expect the type of BaseFoodObject to be?
This is what the ModelBinder is doing - it's creating a new RestaurantViewModel.
As your post action method's signature does not include anything to do with Bread - all the bread properties are ignored.
Some options:
Check for the food properties after binding and read them manually (probably the quickest+easiest but not very "mvc-ish")
public ActionResult Post(RestaurantViewModel viewModelPost)
{
if (!string.IsNullOrEmpty(Request.Form["Unit"]))
// it's a bread form
to make this easier, you could provide a hidden field with the type
if (Request.Form["Type"] == typeof(Bread).Name)
{
var bread = new Bread { Unit = Request.Form["Unit"] }
Add bread to the action so it's bound
public ActionResult Post(RestaurantViewModel viewModelPost, Bread bread)
but then, obviously, it won't work for milk.
So could extend this using an ActionNameSelector to select the correct action
public ActionResult PostBread(RestaurantViewModel viewModelPost, Bread bread)
public ActionResult PostMilk(RestaurantViewModel viewModelPost, Milk milk)
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class FoodSelectorAttribute : ActionNameSelectorAttribute
{
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
... check if provided parameters contains bread/milk
(related link but not a solution to this specific case)
Another option might be to change the Restaurant type to a generic, but would require a few more changes (and ideally use of interfaces), and more details (provided here as an idea, rather than a solution)
The basics would be:
public class RestaurantViewModel<T>
where T: Food
{
}
public ActionResult Post(RestaurantViewModel<Bread> viewModelPost)
public ActionResult Post(RestaurantViewModel<Milk> viewModelPost)
but I've not confirmed if the default ModelBinder would work in this case.
The problem comes with the post. Once you post, all you have is a set of posted data and a parameter of type, RestaurantViewModel. The modelbinder sets all the appropriate fields on Food because that's all it knows. Everything else is discarded. There's nothing that can be done about this. If you need to post fields related to Bread then the type of your property must be Bread. That's the only way it will work.

reusing view with multiple models in Sitecore MVC using glassmapper

I'm having trouble determining how I might reuse views in my sitecore 8 solution using glassmapper. I have two object that contain the same data, but the fields are named differently depending on the template. Title vs Heading, for instance.
This is relatively simple when the page editor isn't a factor as I can just map/cast one object to another, but this seems to choke up the page editor.
The most obvious path is to use Interfaces and extension methods as described here, but this requires creating a real kitchen sink of an Interface.
Any other suggestions? I'd hate to copy the view just to change what properties it uses.
If you are using Controller Renderings then you could define an interface like so:
public interface ITitleAndBody : IGlassBase
{
string Title {get; set;}
string Body {get; set;}
}
And concrete types like so:
public class MyType1 : GlassBase, ITitleAndBody
{
[SitecoreField("Title")]
public virtual int Title {get; set;}
[SitecoreField("Body")]
public virtual string Body {get; set;}
}
public class MyType2 : MyType1
{
[SitecoreField("Header")]
public override int Title {get; set;}
}
You will need two controller actions pointing to the same view:
public ActionResult MyType1()
{
return PartialView("/Views/MyView.cshtml", SitecoreContext.GetCurrentItem<MyType1>());
}
public ActionResult MyType2()
{
return PartialView("/Views/MyView.cshtml", SitecoreContext.GetCurrentItem<MyType2>());
}
And in the view itself use the interface as the model:
#inherits Glass.Mapper.Sc.Web.Mvc.GlassView<ITitleAndBody>
<div>#Editable(Model, x => x.Title)</div>
<p>#Editable(Model, x => x.Body)</p>
This is untested code. It would have been nicer to use View Renderings of course.
Agree to what suggested by jammykam in his comment above - the most straightforward would be to rename template fields to be the same.
So, as per your example they will be all physically called Title; for content editors you may use help display name for particular field, so the will still see them as Title vs. Heading, despite in fact they both are called Title for the system. Of course, if your solution can allow renaming like that... More about this approach by the link: http://goo.gl/b28s2y (last part).
Another option to consider - you may think about proxy objects on top of your glass models that would re-map your field names to whatever you want. Thus passing your proxy objects having same names to a view.
But, right you are, in hardest case probably interfaces would be an answer
Alternately, if the fields are same, you can change their titles on template's field properties. That way when I create an item of template X I would be presented with Field's title assigned on template X while the FieldName itself remains same.
But if I have to do it, I would prefer to use Interfaces with Glass.

How to transfer value from a DropDownList to a property of a serialized model object

I'm testing some concepts in ASP.NET MVC multisteps (Style Wizards) with a small application which allow me to records organizations in a database.
To make things easier, I've a class OrganizationFormModelView that contains an object of class Organization and a property called ParentOrgList of SelectList type. The only purpose of the selectList property is to be used by a DropDownList.
I've also serialize OrganizationFormModelView to get the multisteps Wizard effect.
In my first view (or first step), I use a dropdownlist helper to assign a value to one of the the Organization's property called ParentOrganization, which draws data from the ParentOrgList.
...
<% = Html.DropDownList("Organization.ParentOrganization", Model.ParentOrgList)%>
...
The first time the page loads, I'm able to make a choice. And, my choice is reflected in my object Model all along the wizard' steps(see Visual studio in debugging mode).
But, when any time I'm redirected back to the first view (first step), I get the following error message:
"The ViewData item with the key 'Organization.ParentOrganization' is of type 'System.String' but needs to be of type 'IEnumerable'."
Thanks for helping
After considering carefully my code, I understand now what's going on. OrganizationFormModelView is the class that is being serialized, and here's its definition.
[Serializable]
public class OrganizationFormViewModel
{
public Organization Organization { get; set; }
[NonSerialized]
public SelectList ParentOrgList = null;
public OrganizationFormViewModel(Organization organization, SelectList cList)
{
Organization = organization ?? new Organization();
ParentOrgList = pList;
}
}
From that, I've concluded that, After each serialization process, ParentOrgList is set to null, so I need to find a way of re-assigning value to it. So, below is what I did:
public ActionResult CreateOrganization(string nextButton)
{
//Omitted for brievety
if (formViewModel.ParentOrgList == null)
formViewModel.ParentOrgList = repository.CommuneList;
//Omitted for brievety
}
I also, modified the View so that, even if the value of the ParentOrgList is continuously re-assigned, but the DropDownList keeps the user's choice. So, I choose an Helper overload with default value.
...
<% = Html.DropDownList("Organization.ParentOrganization", Model.ParentOrgList,
Model.Organization.ParentOrganization)%>
...
Now, everything is working perfectly.
However, If someone knows how to proceed differently with the Serialization business, it'd be helpful to share.
Thanks

ASP.NET MVC based CMS - dynamic generation of form helpers

I am working on an ASP.NET MVC based CMS that presents a rather extreme case. The system must allow the user to add custom content types based on different fields, and for every field, one can add options and validations. The thing is that everything is stored in a complex DB and extracted at runtime using LINQ.
I am pretty fresh with ASP>NET MVC so the following dilemma came to mind
How should I make the content creation view so that form helpers are not predefined int he view code but are loaded based on the type of the field ? Do I have to create a factory class that checks the value of the type property of the field, and then returns a helper based on that or there's a better way to do it. This one seems pretty rigid to me , because anytime I make a change in the Fieldtypes table, I will have to make sure to create a check for that new type too.
public class CType {
string Name; //e.g Post Article etc
List<ContentData> data ;
...
}
public class ContentData {
string Data; // Basically this is the data stored for each field
FieldInstance fieldInstance;
...
}
public class FieldInstance {
string Title; // e.g Title Body etc.
FieldType Type ; // e.g textbox textarea image checkbox etc
...
}
public class FieldType {
string Type; // e.g textbox textarea image checkbox etc
...
}
I see an HTML Helper in your future. The HTML Helper can work through your Model at runtime and output the appropriate HTML for the View. If you go that route, I suggest you get to know the StringBuilder and TagBuilder classes. They'll simplify things and help make your HTML Helper much more readable.
I did not know about the concept of templated helpers. This is what happens when you're new to something. Pretty much, this is what fixed my problem
http://msdn.microsoft.com/en-us/library/ee308450%28VS.100,printer%29.aspx

Action property of interface type

With my understading, the nature of a Action is that properties can be pushed w/ request parameter values. And, one wonderful feature is that Struts2 allows you to directly populate parameter values against Class type property ;)
Assuming there exists a Action and property class as below,
class Action extends ActionSupport {
User user;
#Action(value="hello" {#result=(.......)})
public void execute() {
........
}
.....
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return this.user;
}
}
class User {
String name;
.....
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
you could populate User class property by doing like this.
http://...../hello.action?user.name=John or via jsp page
Then, I realize that there are actually people make an Action property as a Interface type. My question is what is the reason behind this. If there is a sample code demonstrating it will be great.
Thanks in advance!
Sorry, but your question does not make much sense.
To clarify:
"Properties": in Java a "property" of a class is something that is accesible via getter/setters method (setXXX() / getXXX() => property XXX), tipically (but not necessarily) corresponds to a private field.
In Struts2 you have an Action object and typically (not necessarily, not always) the properties are populated (set) from the request (via the "Parameters" interceptor), and later in the view stage read from the JSP (or whatever) page.
So, in your example, for the request http://...../hello.action?user.name=John , Struts2 would try to find in your action (...actually in your value stack) a property "user" which has a property "name", and try to set it (if the types are convertible). That is, he would try to call something like yourAction.getUser().setName("John") . Struts2 does not know -does not care- what type are the properties "User" or "Name", even if they are real fields or not. (They are expected to behaviour as "beans", though: i.e. they should have a default constructor).
Why and when you should code interfaces instead of concrete classes is something that is explained in any Java book, it's just a standard good practice and there are tons of pages about it. It has nothing to do with Struts2. In this context, for an Action, one is tipically only interested in doing so for some "service" fields, objects that are typically long-lived (perhaps singletons), are not instantiated by the action itself (nor by the request!). So, those interfaces are NOT the properties we are considering here, they ( usually ) are not exposed publically and usually are not populated nor read from the client.

Resources