简体   繁体   English

ASP.NET MVC结构。 从视图中调用控制器

[英]ASP.NET MVC Structure. calling a controller from a view

hello please help me understand this, and lets hope it help others too. 你好,请帮助我理解这一点,并希望它也能帮助别人。

in MVC what I Understood is a Model–view–controller which should be a pattern if am right. 在MVC中,我理解的是模型 - 视图 - 控制器,如果是正确的,它应该是一个模式。 and in my mind its like: 在我看来它像:

Class in the Model Function in the Controller Layout or output in the view 控制器布局中的模型功能中的类或视图中的输出 ASP.NET MVC结构

but what I couldn't figure out is how to make them communicate or knowing "best practice" so I came up with this simple idea to make me understand. 但我无法弄清楚的是如何让他们沟通或了解“最佳实践”,所以我想出了这个简单的想法让我明白。 I have a simple video "MP4 for reference" on (C:\\Users\\Me\\Documents\\FunVid.mp4) 我有一个简单的视频“MP4供参考”(C:\\ Users \\ Me \\ Documents \\ FunVid.mp4)

and I have created an ASP.NET MVC project including: - A Model. 我创建了一个ASP.NET MVC项目,包括: - 一个模型。 "VideoProdcast.cs" - A Controller. “VideoProdcast.cs” - 一个控制器。 "VideoController.cs" - View "Razor". “VideoController.cs” - 查看“Razor”。 "VideoView.cshtml" “VideoView.cshtml”

so if this is the : 所以如果这是:

Model : "VideoProdcast.cs" 型号:“VideoProdcast.cs”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Vidi.Models
{
    public class VideoProdcast
    {

        public int MovieId { get; set; }
        public string MovieName { get; set; }
    }
}

Controller:"VideoController.cs" 控制器: “VideoController.cs”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidi.Models;

namespace Vidi.Controllers
{
    public class VideoController : Controller
    {
         // GET: Video
         public ActionResult VideoView()
         {
             var Movie = new Movie() { Name = "Movie" };
             return View(Movie);
         }
    }
}

View: "VideoView.cshtml" 查看:“VideoView.cshtml”

@{
ViewBag.Title = "VideoView";
}

<h2>VideoView</h2>

So given the fact that my movie is on (C:\\Users\\Me\\Documents\\FunVid.mp4) what is the best practise to show this movie on my view, using(MVC Structure) the view to call funVid.mp4 as a controller or function by movie name from the model. 因此,考虑到我的电影开启的事实(C:\\ Users \\ Me \\ Documents \\ FunVid.mp4),在我的视图中显示此电影的最佳做法是什么,使用(MVC结构)视图将funVid.mp4称为控制器或按模型中的电影名称功能。 "and please do help me if am wrong in stating anything?" “如果我说错了,请帮助我吗?” Note that video format is not a must "any supported formate will work just fine ". 请注意,视频格式不是必须的“任何支持的格式都可以正常工作”。

I dont know if I made it easier or harder to understand but this is how am imagining it. 我不知道我是否更容易理解,但这就是想象它。 thanks in advance I really do appreciate your help. 在此先感谢我真的很感谢你的帮助。

Not sure if this is a best practice: 不确定这是否是最佳做法:

  1. Add URL to your model as string. 将URL作为字符串添加到模型中。

  2. In your controller, populate your model with the path to your video, from wherever they are stored, or simply 在您的控制器中,使用视频路径填充模型,无论它们存储在何处,或者只是

    var Movie = new VideoProdCast() { NameName = "Movie", URL = "C:\\yourpath\\yourvideo.mp4", id = 1 }; var Movie = new VideoProdCast(){NameName =“Movie”,URL =“C:\\ yourpath \\ yourvideo.mp4”,id = 1}; return View(Movie); 返回视图(电影);

  3. In your view, set your video src=model.url, also reference the model at the top of the view. 在您的视图中,设置视频src = model.url,同时引用视图顶部的模型。

You're very much on the right track. 你正走在正确的轨道上。 The two primary things you are missing, are the path to the file, as you stated, and completing the model binding you started. 您缺少的两个主要内容是文件的路径,如您所述,并完成您开始的模型绑定。

As for the file path, this should be a property of the VideoProdcast Model: 至于文件路径,这应该是VideoProdcast模型的属性:

public class VideoProdcast
{
    public int MovieId { get; set; }
    public string MovieName { get; set; }
    public string MoviePath { get; set; }
}

Then, add the following line to your View: 然后,将以下行添加到您的视图:

@model Vidi.Models.VideoProdcast

This gives you access to the Movie model object that you passed to the view in the controller ( return View(Movie); ). 这使您可以访问传递给控制器​​中视图的Movie模型对象( return View(Movie); )。 Since you've added the path to the file, you can now add something like this to your view: 由于您已添加文件路径,因此您现在可以在视图中添加以下内容:

<video src="@Model.MoviePath">@Model.MovieName</video>

or something like that to show the video on the page. 或类似的东西在页面上显示视频。

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

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