This is a migrated thread and some comments may be shown as answers.

In MVC View, I need to pass value from within the model to the controller from a KendoUI Widget

3 Answers 452 Views
Upload
This is a migrated thread and some comments may be shown as answers.
L
Top achievements
Rank 1
L asked on 29 Aug 2014, 06:52 PM
The code below works. The controller receives the value "Test Value" when the Upload widget is executed.However, I have a key value from the model that is displayed within this view that I need to send instead of the hardcoded text.

Note. This is a custom template within a grid popup editor.

Widget

ViewBag.Title = "Test Value"
@(Html.Kendo().Upload() .Name("files") .TemplateId("fileTemplate") .Async (a => a .Save("Save", "OpenRecords"), new { MyRequest = ViewBag.Title }) .AutoUpload(true)) )


Controller

public ActionResult Save(IEnumerable<HttpPostedFileBase> files, string MyRequest)
{
// The Name of the Upload component is "files"

if (files != null)
{
foreach (var file in files)
{
// Some browsers send file names with full path.
// We are only interested in the file name.

var fileName = Path.GetFileName(file.FileName);
var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);
// The files are not actually saved in this demo
file.SaveAs(physicalPath);
ViewBag.FileName = fileName;
//return Content(physicalPath);

}

}

// Return an empty string to signify success
return Content("");


}

3 Answers, 1 is accepted

Sort by
0
Dimiter Madjarov
Telerik team
answered on 01 Sep 2014, 07:29 AM
Hello David,


I covered this question in the support thread on the same topic. You could try using the following approach.

I wish you a great day!

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
L
Top achievements
Rank 1
answered on 02 Sep 2014, 01:58 PM
I'm able to get this method to work fine within a normal cshtml file. 

But it does not work within a Custom Grid Popup Editor template.

As soon as I attach the ".Upload" event to the Upload widget within the custom template...it breaks the template and the grid no longer functions right and the buttons on the Custom Template no longer work. 

The Upload widget works fine in the Custom Template and successfully send the File up to the controller UNLESS I add the "Upload" event.   It doesn't matter what the event handler has in it.  

 
CUSTOM TEMPLATE WITH UPLOAD EVENT..... DOES NOT WORK:

<h2>Open Records Request</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

        <div class="edit-field">

            <section style="border-top:double; padding-bottom:5px;"/>

            @Html.LabelFor(model => model.RequestID, "Requested ID", new { style = "color:#212a33" })
            @Html.EditorFor(model => model.RequestID, new { style = "width: 40%; font-size:18px; font-family:inherit" })
            @Html.ValidationMessageFor(model => model.RequestID)


            @Html.LabelFor(model => model.CaseRequested, "Report Requested", new { style = "color:#212a33" })
            @Html.TextBoxFor(model => model.CaseRequested, new { style = "width: 40%; font-size:18px; font-family:inherit" })
            @Html.ValidationMessageFor(model => model.CaseRequested)

            @Html.LabelFor(model => model.Organization, "Organization", new { style = "color:#212a33" }))
            @Html.TextBoxFor(model => model.Organization, new { style = "width: 30%; font-size:18px; font-family:inherit" })
            @Html.ValidationMessageFor(model => model.Organization)
        </div>

        <div class="edit-field">

            @Html.LabelFor(model => model.DateReceived, "Date Received", new { style = "color:#212a33" }))
            @Html.EditorFor(model => model.DateReceived)
            @Html.ValidationMessageFor(model => model.DateReceived)

            @Html.LabelFor(model => model.DateFulfilled, "Date Fulfilled", new { style = "color:#212a33" }))
            @Html.EditorFor(model => model.DateFulfilled)
            @Html.ValidationMessageFor(model => model.DateFulfilled)

            @Html.LabelFor(model => model.Notes, "Notes", new { style = "color:#212a33" })
            @Html.EditorFor(model => model.Notes)
            @Html.ValidationMessageFor(model => model.Notes)
       
            @Html.LabelFor(model => model.FilePath, "Document", new { style = "color:#212a33" })
            @Html.EditorFor(model => model.FilePath)
       

            
            @(Html.Kendo().Upload()
            .Name("files")
            .TemplateId("fileTemplate")
            .Async
                (a => a
                    .Save("Save", "OpenRecords")
                    .AutoUpload(true))
             .Events
                (events => events
                    .Error("onError")
                    .Upload("onUpload")
                    )
            )

</div>

    
}
    

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")



        <script>

            function onError(e) {
                KendoConsole.log("Error ( " + e.operation );
            }


            function onUpload(e) {
                e.data = {
                   MyRequest: $("#RequestID").val()
                };
            }

        </script>

    }


GRID THAT REFERENCES THE CUSTOM TEMPLATE


@(Html.Kendo().Grid<sgrc.vpd.home.Models.vwOpenRecords>()
    .Name("grid")
    .Selectable(selectable => selectable.Mode(GridSelectionMode.Single).Type(GridSelectionType.Row))
    .Columns(columns =>
    {
        columns.Bound(dataOpenRecords => dataOpenRecords.RequestID).Hidden(true);
        columns.Bound(dataOpenRecords => dataOpenRecords.DateReceived).Title("Request Received").Format("{0:MM/dd/yyyy}").Width(80);
        columns.Bound(dataOpenRecords => dataOpenRecords.Requestor).Title("Requestor").Width(200);
        columns.Bound(dataOpenRecords => dataOpenRecords.Organization).Title("Organization").Width(200);
        columns.Bound(dataOpenRecords => dataOpenRecords.CaseRequested).Title("Record Requested").Width(200);
        columns.Bound(dataOpenRecords => dataOpenRecords.DateFulfilled).Title("Request Fulfilled").Format("{0:MM/dd/yyyy}").Width(110);
        columns.Bound(dataOpenRecords => dataOpenRecords.Notes).Title("Notes").Width(300);
        columns.Bound(dataOpenRecords => dataOpenRecords.FilePath).Title("Attachment").Width(250)
            .ClientTemplate("<a href='http://tooms/WcfReports/" + Url.Content("#= FilePath #") + "' target='_blank'>#= FilePath #</a>");
        columns.Command(commands =>
              {
                  commands.Destroy();
                  commands.Edit();
              }).Title("Commands").Width(210);
    })
    .ToolBar(toolbar =>
        {
            toolbar.Create();
            //toolbar.Save();
        })


    .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("OpenRecordsTemplate"))
    .DataSource(dataSource => dataSource
    .Ajax()
    .Events(e => e.Error("onError")) // Handle the "error" event (errors set in controller)
    .Batch(true)
    .ServerOperation(false)  // Paging, sorting, filtering and grouping will be done client-side

        
        .Model(model =>
            {
                model.Id(dataOpenRecords => dataOpenRecords.RequestID);
                model.Field(dataOpenRecords => dataOpenRecords.RequestID).Editable(false);
                model.Field(dataOpenRecords => dataOpenRecords.FilePath).Editable(false);
            })
            
        .Create(create => create.Action("OpenRecords_Create", "OpenRecords"))
        .Read(read => read.Action("OpenRecords_Read", "OpenRecords"))
        .Update(update => update.Action("OpenRecords_Update", "OpenRecords"))
        .Destroy(destroy => destroy.Action("OpenRecords_Destroy", "OpenRecords"))
        )

        .AutoBind(true)
        .Sortable()
        .Navigatable()
        .Events(e => e.Edit("onChange"))
        .HtmlAttributes(new {style = "width:1160px;"})
     .Filterable()
      
)
0
Dimiter Madjarov
Telerik team
answered on 02 Sep 2014, 02:04 PM
Hello David,


I answered this question in the support thead on the same topic. For convenience lets continue the discussion in only one of the threads - forum or support.

Thanks for the understanding.

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Upload
Asked by
L
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
L
Top achievements
Rank 1
Share this question
or