简体   繁体   English

将日期从局部视图输入到控制器

[英]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. 美好的一天,我有一个具有文档部分的应用程序,还有一个DocumentViewer视图,其中包含文档共享的所有内容,例如保存和徽标,等等。然后,每个文档都将作为局部视图创建。 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 保存进入文档控制器中的DocumentSaver ActionResutl,但从DocumentViewer视图触发

the date in this instance is entered in the partialView _LeaveReq in this format 此实例中的日期以这种格式输入partialView _LeaveReq中

How would I pass that date from that partial view into "DocumentSaver","Document" 我如何将该日期从该局部视图传递到“ 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. 我看了很多例子,看了脚本和Ajax调用,但是它们都是从一个视图发回的,不是真正的局部视图,或者它们使用模型来回传递数据,但这是文档中的一个条目。并没有真正关联到模型。

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 . 不管您在哪里生成日期时间都没关系,可以说您在部分视图上生成了日期时间,并使用Html.Partial("_partialview")在视图中引用了部分视图,并显示在<div><div>一个id并使用jquery从中获取值,然后使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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