Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Desley 44 posts 169 karma points
    Mar 27, 2014 @ 12:08
    Desley
    0

    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:

    <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>
    

    Sorry for the long post but a little summary:

    1. How do I get the value from the frontend dropdown list i.e. model.Frequentie

    2. How do I set a value in a Umbraco backend dropdown list

    3. 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.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Mar 27, 2014 @ 12:11
    Jeroen Breuer
    1

    Hello,

    Maybe this example from the media service can help a little bit: https://gist.github.com/nul800sebastiaan/8008892

    Jeroen

  • Desley 44 posts 169 karma points
    Mar 27, 2014 @ 13:28
    Desley
    0

    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);
    

    But how do I get the image from this

    <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>
    

    Into the < MyImageHere>.

  • Desley 44 posts 169 karma points
    Mar 28, 2014 @ 09:16
    Desley
    0

    Anyone?

Please Sign in or register to post replies

Write your reply to:

Draft