简体   繁体   中英

getting date entered from partial view to controller

Good Day, I have an application that has a documents section, there is a DocumentViewer view that has all the things the documents share like save and logo and so on... Then each document is created as a partial view. on Save I need to get a date that was entered by the user.

Save goes to the DocumentSaver ActionResutl in the Document Controller, but is triggered from the DocumentViewer View

the date in this instance is entered in the partialView _LeaveReq in this format

How would I pass that date from that partial view into "DocumentSaver","Document"

I have looked at many examples, and looked at scripts and ajax calls, but they all post back from a view, not really partial view, or they make use of models to pass data back and forth, but this is an entry on a document that is not really connected to a model.

It really doesn't matter where you generate the datetime lets say you have generated it on your partial view and you have referenced your partialview in your View using Html.Partial("_partialview") and it is displayed in a <div> give that <div> an id and get the value from it using jquery and pass it over to your controller using ajax . see the example below:

The Partial:

<div id="date">Model.Date.ToString()</div>

The View:

<div class="container">
  @html.Partial("_partialview")
</div>
//you have to include the JQuery within the view not PartialView

<script>
    $(document).ready(function(e) {
        e.preventDefault();

        var _date = $("#date").text();
        $.Ajax({
            type: 'Post',
            url: "Action/Controller",
            data: {
                date: _date
            },
            success: function(data)
            {
                //action
            },
            error: function(data)
            {
                //action
            }

        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM