Creating umbraco file with Image Upload and Dropdown values
Hello everyone,
I have the following code to create a file in my SurfaceController:
var contentService = Services.ContentService;
var advertentie = contentService.CreateContent(string.Format("{0}-{1}", model.Frequentie, DateTime.Now.ToShortDateString()), 1068, "AdvertentieItem", 0);
advertentie.SetValue("titel", model.Titel);
advertentie.SetValue("frequentie", model.Frequentie);
advertentie.SetValue("opmerking", model.Opmerking);
advertentie.SetValue("status", "Open");
advertentie.SetValue("provincie", model.Provincie);
advertentie.SetValue("stad", model.Stad);
advertentie.SetValue("postcode", model.Postcode);
advertentie.SetValue("adres", model.Adres);
advertentie.SetValue("beschrijving", model.Beschrijving);
contentService.SaveAndPublishWithStatus(advertentie, 0, true);
TempData.Add("Status", "Your form was successfully submitted at " + DateTime.Now);
return RedirectToCurrentUmbracoPage();
The ViewModel (Obviously some types have to change here but I have no idea to what):
public class AdvertentieModel
{
[Required, Display(Name = "Titel")]
public string Titel { get; set; }
[Required, Display(Name = "Beschrijving")]
public string Beschrijving { get; set; }
[Required, Display(Name = "Frequentie")]
public string Frequentie { get; set; }
[Required, Display(Name = "Opmerking")]
public string Opmerking { get; set; }
[Display(Name = "Image")]
public string Image { get; set; }
[Required, Display(Name = "Provincie")]
public string Provincie { get; set; }
[Required, Display(Name = "Stad")]
public string Stad { get; set; }
[Required, Display(Name = "Postcode")]
public string Postcode { get; set; }
[Required, Display(Name = "Adres")]
public string Adres { get; set; }
}
And the view:
@using (Html.BeginUmbracoForm("_AdvertentieAanmaken", "AdvertentieAanmakenSurface"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div id="AanmaakBox">
<h2 id="center">Advertentie aanmaken</h2>
<table>
<tr>
<td>@Html.DisplayName("Titel:")</td>
<td>@Html.TextBoxFor(m => m.Titel)</td>
</tr>
<tr>
<td>@Html.DisplayName("Beschrijving:")</td>
<td>@Html.TextAreaFor(m => m.Beschrijving)</td>
</tr>
<tr>
<td>@Html.DisplayName("Frequentie:")</td>
<td>
@Html.DropDownListFor(m => m.Frequentie, new SelectList(new List<Object>{
new { value = 0 , text = " < Maak een keuze > " },
new { value = 1 , text = "Eenmalig" },
new { value = 2 , text = "Dagelijks" },
new { value = 3 , text = "Weekelijks"},
new { value = 4 , text = "Maandelijks"},
new { value = 5 , text = "Anders"}
}, "value", "text", 0))
</td>
</tr>
<tr>
<td>@Html.DisplayName("Opmerking:")</td>
<td>@Html.TextBoxFor(m => m.Opmerking, new { @placeholder = " bijvoorbeeld: derde donderdag van de maand" })</td>
</tr>
<tr>
<td id="ImageUpload">@Html.DisplayName("Afbeelding:")</td>
<td id="ImageUpload">
<div class="fileinput fileinput-new" data-provides="fileinput">
<span class="btn btn-default btn-file"><span class="fileinput-new">Select file</span><span class="fileinput-exists">Change</span><input type="file" name="..."></span>
<span class="fileinput-filename"></span>
<a href="#" class="close fileinput-exists" data-dismiss="fileinput" style="float: none">×</a>
</div>
</td>
</tr>
</table>
<br />
</div>
<br />
<div id="AanmaakBox">
<h2 id="center">Locatie</h2>
<img id="InformationHover" data-trigger="hover" src="/images/icons/information.png" />
<table>
<tr>
<td>@Html.DisplayName("Provincie:")</td>
<td>
@Html.DropDownListFor(m => m.Provincie, new SelectList(new List<Object>{
new { value = 0 , text = " < Maak een keuze > " },
new { value = 1 , text = "Drenthe" },
new { value = 2 , text = "Flevoland" },
new { value = 3 , text = "Friesland"},
new { value = 4 , text = "Gelderland"},
new { value = 5 , text = "Groningen"},
new { value = 6 , text = "Limburg"},
new { value = 7 , text = "Noord-Brabant"},
new { value = 8 , text = "Noord-Holland"},
new { value = 9 , text = "Overijssel"},
new { value = 10 , text = "Utrecht"},
new { value = 11 , text = "Zeeland"},
new { value = 12 , text = "Zuid-Holland"}
}, "value", "text", 0))
</td>
</tr>
<tr>
<td>@Html.DisplayName("Stad:")</td>
<td>@Html.TextBoxFor(m => m.Stad)</td>
</tr>
<tr>
<td>@Html.DisplayName("Postcode:")</td>
<td>@Html.TextBoxFor(m => m.Postcode)</td>
</tr>
<tr>
<td>@Html.DisplayName("Adres:")</td>
<td>@Html.TextBoxFor(m => m.Adres)</td>
</tr>
<tr>
<td></td>
<td>
<input class="btn btn-custom pull-right" type="submit" value="Aanmaken" />
<a class="pull-right" id="AanmakenAnnuleren" href="/Advertenties/">Annuleren</a>
</td>
</tr>
<tr>
<td><p class="field-validation-error">@TempData["Status"]</p></td>
</tr>
</table>
</div>
}
Now here are the issues I am having. Firstly model.Frequentie is empty. I am assuming this has something to do with the fact I have it as a string in my model but I couldn't find anywhere what to change it to (if that is even the problem).
Secondly if I try to set the value manually, like with Status to Open it doesn't get set in Umbraco (which is a dropdown list in Umbraco).
advertentie.SetValue("status", "Open");
And thirdly, how do I upload the image I get from the form here to Umbraco:
Ok so presumably I use this code from the example:
var ms = Services.MediaService;
var mediaImage = ms.CreateMedia("test", -1, "Image");
mediaImage.SetValue("umbracoFile", "<MyImageHere>");
ms.Save(mediaImage);
Creating umbraco file with Image Upload and Dropdown values
Hello everyone,
I have the following code to create a file in my SurfaceController:
The ViewModel (Obviously some types have to change here but I have no idea to what):
And the view:
Now here are the issues I am having. Firstly model.Frequentie is empty. I am assuming this has something to do with the fact I have it as a string in my model but I couldn't find anywhere what to change it to (if that is even the problem).
Secondly if I try to set the value manually, like with Status to Open it doesn't get set in Umbraco (which is a dropdown list in Umbraco).
And thirdly, how do I upload the image I get from the form here to Umbraco:
Sorry for the long post but a little summary:
How do I get the value from the frontend dropdown list i.e. model.Frequentie
How do I set a value in a Umbraco backend dropdown list
How do I upload the image to Umbraco
I know this is a lot to ask but I hope someone can spend the time to help me out.
Hello,
Maybe this example from the media service can help a little bit: https://gist.github.com/nul800sebastiaan/8008892
Jeroen
Ok so presumably I use this code from the example:
But how do I get the image from this
Into the < MyImageHere>.
Anyone?
is working on a reply...