Hey guys, I have a property and partial view on a textpage I would like a property from the textpage within the partial view. Can I do this, will this break my MVC view model?
Basically I want to pre-populate a text area within a form on my partial with data from a rich text editor on the textpage. This is so the content is editable by the content managers.
You should be able to do that by simply writing @Model.Content.GetPropertyValue("yourRteAlias"); in your partial view macro if you're using strongly typed for instance.
Just starting out with MVC but my first issue is I can pass page elements to my partials views like this. It works.
but when I use partialviewmacros, in the hope of utilizing htmlactions within surface controllers to share model content between models it fails.
in the above code, you can see i have things wired up to use either partialViews or render view via a partialViewMacro. Both render however when utilising partialViewMacros it doesnt find the thankyoupage or disclaimer content from the page, therefore doesnt pass the required strings into the new object.
Here is a copy of my primary contactSurfaceController...on init of the form this should accept the newly created ContactModel object, as passed from the templatepage, where it gathers requiredPropertys (disclaimer and thankyoupage) from the content tree. Once it has these stored as temp within the ContactModel I can then manipulate content utitlizing DOM or Jquery on user input of form. My plan is to then create a new child disclaimer model which may be in a child relationship to the contactModel and pass the manipulated/updated contract/disclaimer into the database via petapoco/email from there. Hope I still have you ;)
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Text;
using umbraco;
using Kitetrippin.logic.Models;
namespace Kitetrippin.logic.Controllers
{
public class ContactSurfaceController : SurfaceController
{
[ChildActionOnly]
public ActionResult ContactForm(ContactModel model)
{
/*var model = new ContactModel();*/
//Initialize model however you want
/*model.FullName = "Enter your name";*/
//In case you need to access the current node
/*var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault());*/
//In case you need to access the home node
/*var home = currentNode.AncestorsOrSelf(0).First();*/
return PartialView("ContactForm", model);
}
[HttpPost, ValidateInput(false)]
public ActionResult Contact(Kitetrippin.logic.Models.ContactModel model)
{
if (ModelState.IsValid)
{
/*ApplicationContext.DatabaseContext.Database.Save(new Contact());*/
//Create new Contact object
var contactToAdd = new Kitetrippin.logic.Pocos.Contact();
//Set values from view model & grab the current node ID
contactToAdd.ContactId = UmbracoContext.PageId.Value;
contactToAdd.FirstName = model.FirstName;
contactToAdd.NickName = model.NickName;
contactToAdd.LastName = model.LastName;
contactToAdd.Age = model.Age;
contactToAdd.Email = model.Email;
contactToAdd.Phone = model.Phone;
contactToAdd.Message = model.Message;
//tempdisclaimer only tempdata.
contactToAdd.Disclaimer = model.Disclaimer;
//Get the Umbraco db
var db = ApplicationContext.DatabaseContext.Database;
//Add the object to the DB
db.Insert(contactToAdd);
//create new contact registration email.
var sb = new StringBuilder();
sb.AppendFormat("<h1>Kitetrippin New Student Pre-registration</h1>", model.FirstName);
sb.AppendFormat("<p>First Name: {0}</p>", model.FirstName);
sb.AppendFormat("<p>Nick Name: {0}</p>", model.NickName);
sb.AppendFormat("<p>Last Name: {0}</p>", model.LastName);
sb.AppendFormat("<p>Age: {0}</p>", model.Age);
sb.AppendFormat("<p>Email: {0}</p>", model.Email);
sb.AppendFormat("<p>Phone: {0}</p>", model.Phone);
sb.AppendFormat("<p>{0}</p>", model.Message);
//tempdisclaimer only to database to change
sb.AppendFormat("{0}", model.Disclaimer);
library.SendMail("[email protected]","[email protected]","KiteTripping - new Student Contact Form",sb.ToString(),true);
/*return RedirectToUmbracoPage(model.ThankYouPage);*/
//See here - At this point, I can initialise a Disclaimer model
//pass disclamer template(prepropulated inline html)
//from tempstore(contact model) into the newly created Disclaimer object.
var disc = new Kitetrippin.logic.Models.DisclaimerModel();
disc.disclaimer = model.Disclaimer;
//From here I can return a partial view, which renders using the macro :) avoiding templating issues with a normal view.
return PartialView("DisclaimerForm", disc);
/*return CurrentUmbracoPage();*/
/* alternative is inheriting from render model using DisclaimerImportModel approach.*/
}
return CurrentUmbracoPage();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Text;
using umbraco;
using Kitetrippin.logic.Models;
namespace Kitetrippin.logic.Controllers
{
public class DisclaimerSurfaceController : SurfaceController
{
[ChildActionOnly]
public ActionResult DisclaimerForm(DisclaimerModel model)
{
string temp = model.disclaimer;
/*var model1 = new DisclaimerModel();*/
//Initialize model however you want
/*model.FullName = "Enter your name";*/
//In case you need to access the current node
/*var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault());*/
//In case you need to access the home node
/*var home = currentNode.AncestorsOrSelf(0).First();*/
return PartialView("DisclaimerForm", model);
}
[HttpPost, ValidateInput(false)]
public ActionResult SaveArticle(Kitetrippin.logic.Models.DisclaimerModel model)
{
if (ModelState.IsValid)
{
//Perform some business logic here
//Save Title and Article Html in database.
//contactToAdd.Disclaimer = model.Disclaimer;
return Json(new { IsOK = true });
}
return Json(new { IsOK = true });
}
}
}
accessing parent property within partial view
Hey guys, I have a property and partial view on a textpage I would like a property from the textpage within the partial view. Can I do this, will this break my MVC view model?
Basically I want to pre-populate a text area within a form on my partial with data from a rich text editor on the textpage. This is so the content is editable by the content managers.
I hope this helps.
Hi Matthew
You should be able to do that by simply writing @Model.Content.GetPropertyValue("yourRteAlias"); in your partial view macro if you're using strongly typed for instance.
Looking forward to hearing from you.
/Jan
ok Jan,
Just starting out with MVC but my first issue is I can pass page elements to my partials views like this. It works. but when I use partialviewmacros, in the hope of utilizing htmlactions within surface controllers to share model content between models it fails.
in the above code, you can see i have things wired up to use either partialViews or render view via a partialViewMacro. Both render however when utilising partialViewMacros it doesnt find the thankyoupage or disclaimer content from the page, therefore doesnt pass the required strings into the new object.
Here is a copy of my primary contactSurfaceController...on init of the form this should accept the newly created ContactModel object, as passed from the templatepage, where it gathers requiredPropertys (disclaimer and thankyoupage) from the content tree. Once it has these stored as temp within the ContactModel I can then manipulate content utitlizing DOM or Jquery on user input of form. My plan is to then create a new child disclaimer model which may be in a child relationship to the contactModel and pass the manipulated/updated contract/disclaimer into the database via petapoco/email from there. Hope I still have you ;)
Here is a copy of the form/s and a little glue to make things more clear
is working on a reply...