简体   繁体   English

是否可以在 ASP.NET MVC 的局部视图上使用一些 JavaScript?

[英]Is it possible use some JavaScript on a Partial View in ASP.NET MVC?

I am new on .NET MVC and on StackOverFlow, probably you will disdain on the approach that I am using, but I am a little bit confused.我是 .NET MVC 和 StackOverFlow 的新手,可能你会鄙视我使用的方法,但我有点困惑。

The situation is this: I have three partial view with their own model, these partial view are rendering in a complete view.情况是这样的:我有自己的model的三个局部视图,这些局部视图是完整的视图。 The complete view have a model who contains the three model of the partial views.完整视图有一个 model,其中包含部分视图的三个 model。 A specifique partial view has a form who have to perform an ajax request and send some data to the main controller of the complete view with the goal to perform an action.一个特定的部分视图有一个表单,它必须执行 ajax 请求并将一些数据发送到完整视图的主 controller 以执行操作。

Now, the problem is that everything is rendering in the main page and I need to take the data from the model of the partial view and if I use a script in the main page, the data that I need aren't there.现在,问题是所有内容都在主页中呈现,我需要从局部视图的 model 获取数据,如果我在主页中使用脚本,我需要的数据不存在。 From this problem derive my question.从这个问题得出我的问题。

If you have some advices I will be happy to receive them.如果您有一些建议,我将很乐意接受。 I know is a little bit confusing and if you need other explanation I'll try to give you.我知道这有点令人困惑,如果您需要其他解释,我会尽力给您。

Thank you.谢谢你。

You can do that as follows.您可以按如下方式进行。

Assume you have a model as follows:假设您有一个 model,如下所示:

public class Book
    {
    [Key]
    public int ID { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public decimal Price { get; set; }

}

And you have a parent page called index and two partial views called LoadPartialView1 , LoadPartialView2 are loading on that page.您有一个名为index的父页面和两个名为LoadPartialView1的局部视图, LoadPartialView2正在该页面上加载。

You can user @Html.Action("LoadPartialView1", "Home") to load partial view.您可以使用@Html.Action("LoadPartialView1", "Home")加载部分视图。 Here LoadPartialView1 method on Home controller will be executed.这里将执行Home controller 上的LoadPartialView1方法。

public class HomeController : Controller
    {
public PartialViewResult LoadPartialView1()
        {
            Book book = new Book()
            {
                ID = 1,
                Title = "Book1",
                Description = "Test",
                Price = Convert.ToDecimal(250.00)
            };
            return PartialView("_PartialView1", book);
        }
}

Here you can see that I am passing the book model to the partial view.在这里你可以看到我正在通过书 model 到局部视图。

And here is my _PartialView1 .这是我的_PartialView1

@model WebApplication1.Models.Book

<h4>This is the _PartialView1 Header</h4>
<p id="bookId">Book Id = @Model.ID</p>
<p>Book Title = @Model.Title</p>
<p>Book Descrittion = @Model.Description</p>
<p>Book Price = @Model.Price</p>

And the other scenario.还有另一种情况。 Assume you want to submit an form.假设您要提交表单。 You can simply call controller using an Ajax Call.您可以使用 Ajax 调用来简单地调用 controller。

Loading _PartialView2 from controller as follows:从 controller 加载 _PartialView2 如下:

public PartialViewResult LoadPartialView2()
        {
            Book book = new Book();
            return PartialView("_PartialView2", book);
        }

This is my _PartialView2 view:这是我的_PartialView2视图:

@model WebApplication1.Models.Book

<h4>This is the _PartialView2 Header</h4>
<label>Book Id</label><input id="bookId" type="text" />
<label>Book Title</label><input id="bookName" type="text" />
<label>Book Descrittion</label><input id="bookDesc" type="text" />
<label>Book Price </label><input id="bookPrice" type="text" />
<input id="btnSave" type="button" value="Save"/>

<script type="text/javascript">

    $("#btnSave").click(function () {
        var id = $('#bookId').val();
        var name = $('#bookName').val();
        var desc = $('#bookDesc').val();
        var price = $('#bookPrice').val();

        var mybook = {
            ID: id,
            Title: name,
            Description: desc,
            Price: price

        };

        $.ajax({
            url: "/Home/DataFromPartialView",
            type: "POST",
            data: JSON.stringify(mybook),
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            error: function (xhr) {
                alert('Error');
            },
            success: function (result) {
                alert('Success');

            },
        });

    });

</script>

Here I am getting the data from input fields, create an book object and pass it to the DataFromPartialView method in Home controller.在这里,我从input字段中获取数据,创建一本书 object 并将其传递给 Home controller 中的DataFromPartialView方法。

Here is the method code:下面是方法代码:

public PartialViewResult DataFromPartialView(Book mybook)
        {

            return View();
        }

This way you can pass model data to controller.这样您就可以将 model 数据传递给 controller。

And finally the code of Indec Page which contains the partial views.最后是包含部分视图的 Indec Page 代码。

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
    <div> 
        <p>This is my Home Page.</p>
    </div>
    <div id="partialView1">
       @Html.Action("LoadPartialView1", "Home")
    </div>
    <div id="partialView2">
        @Html.Action("LoadPartialView2", "Home")
    </div>
</body>
</html>

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

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