Html.RenderPartial方法的协助,传递一个带有模型的ascx文件。

huangapple go评论65阅读模式
英文:

Assistance with Html.RenderPartial passing an ascx file with a model

问题

The code you provided appears to be a mixture of ASP.NET MVC and WebForms. In this context, I can provide some insights and potential solutions for the issues you mentioned:

  1. The "Html" and "Model" properties are null:

    • In ASP.NET MVC, the "Html" property should not be null unless something is fundamentally wrong. It's usually accessible through @Html in Razor views. Ensure that your controller is properly passing a model to the view.
    • The "Model" property should also be populated with the data you expect it to contain. Check if your controller is correctly creating and passing the model to the view.
  2. "Object reference not set to an instance of an object" error:

    • This error typically occurs when you're trying to access properties or methods on null objects. Double-check the code in your view and ensure that you have properly initialized the "Html" and "Model" properties in your controller before rendering the view.
  3. Model binding in Menu.ascx:

    • The "Model" in Menu.ascx should be derived from the parent view (the .aspx file that includes Menu.ascx). Verify that the parent view is correctly passing the required model to Menu.ascx.
  4. Model.SecurityItems.Where():

    • This code is likely filtering security items based on certain criteria. Make sure that the "Model" object contains a property named "SecurityItems," and that it's properly populated with data before this code is executed.
  5. Differences between .aspx and .cshtml:

    • It's worth noting that .aspx and .cshtml files have different syntax and concepts. Ensure that you're using the appropriate syntax and conventions for the type of view you're working with.
  6. Debugging:

    • Use debugging tools to inspect the values of "Html" and "Model" properties during runtime to pinpoint the source of the issue.

Without seeing the complete code and configuration, it's challenging to provide a precise solution, but these points should help you troubleshoot and identify the root cause of the problem.

英文:

A site I'm programming is using ASP.NET MVC and WebForms, and I am stuck on how it was previously working (they lost files).

Within their ASPX file they have something along the lines of

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" Inherits="System.Web.Mvc.ViewPage<StudentRepo.UI.Mvc.Models.MenuSecurity>" %>


<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.RenderPartial("~/Menu.ascx", Model); %>


<div class="modal-footer">
                    <button id="btnCloseUpdateStudentType" type="button" class="btn btn-default" data-dismiss="modal" style="width:80px;">Close</button>               
                    <%if (Model.SecurityItems.Where(i => i.ViewPage == "Manage Student Types" && i.Update).Count() > 0)
                        {%>          
                    <button id="btnUpdateStudentType" type="button" class="btn btn-primary" rel="popover" data-placement="top" data-trigger="manual" style="width:80px;">Update</button>   
                    <%} %>                     
                </div>
</asp:Content>

But each time I debug and go to the page, the Html and Model Property are null, throwing the error "Object reference not set to an instance of an object." This is also true if I leave out the model and just RenderPartial on the ascx file.

How is the Html null when the Menu.ascx file is present in that path?

Where is expecting to get the Model from? The view of the Menu.ascx file? I don't understand the use of Model.SecurityItems.Where in the footer either.

I have tried and been stuck on this for weeks, and it seems they are doing it differently than most people since I have done a lot of research.

The views provided are either .aspx or ascx files, so I can't implement it with .cshtml files like I have seen others do.

答案1

得分: 1

A web site built around MVC is vastly different than the architecture of an asp.net webforms site.

So, while you CAN use aspx pages in an MVC site, it's not the default, and you require a good deal of "tricks" to make this work.

The views provided are either .aspx or ascx files.

Those are NOT called views, and you can't make such a statement any more than trying to say a glass of water and a dog are the same thing.

If your site has ZERO cshtml pages now, you do NOT have an MVC site, but you have an asp.net webforms site.

An aspx web page is a web forms page. There are no views in such pages.

You will have an aspx page with markup, and then a corresponding code-behind page.

So, if you have a page called Fighters.aspx, then you will have (in most cases) a corresponding code-behind page called Fighters.aspx.cs (or Fighters.aspx.vb if this is a vb.net site).

There are no cshtml pages in such a site, you can't add them, and they have nothing to do with an asp.net web forms site.

Different design, different architecture, and different operation.

So I can't implement it with .cshtml files.

There are no such types of files in a webforms project.

And keep in mind there are two types of webform projects.

There is an asp.net webforms site.

And then there is what is called an asp.net webforms application.

The big difference is the lack of the word "application."

You can, in about 5 seconds of time, know the difference, and a webforms site will not have the "designer" files like a web application site has.

For a web site, you go File -> Open Web Site.

For a web site application, you go File -> Open Project.

I should also point out that while MVC sites can be either .NET Core (the newer framework), asp.net web sites, or asp.net web site "applications" can ONLY be built using the .NET Framework, and NOT with .NET Core.

So, if there were never any cshtml files in that project, then you do NOT have an MVC site. You can perhaps get some cshtml pages to work, but I don't think you should mess around trying to do that in this case.

As I noted, you CAN load and make aspx pages work in an MVC site, but from what you provided, you don't have an "mvc" site.

However, if your posted code is from that project?

Then it looks like you may well have an MVC site, and it is loading + consuming asp.net "aspx" pages. While this is possible, it is somewhat convoluted approach here.

I would right-click while viewing that aspx web page and choose "View Code" - you should see the code-behind page for that aspx page.

We will likely require more details here, but that posted page of yours does not look correct, and it looks like an asp.net webforms master page, with some extra junk tossed in that does not really look correct.

If some pages in question REALLY do have this:

<% Html.RenderPartial("~/Menu.ascx", Model); %>

Then OK, this means you do have some views and do have some cshtml pages. However, in most cases, the reverse is done. So, if there are in fact some cshtml files in that project, then I stand corrected.

(You may well have a cshtml page and it loads + uses an aspx page or perhaps an ascx control).

I mean, when you open this project (the sln file), does the site build + run when you hit F5?

Edit: The choice to create an MVC or webforms

Let's try this again. I stated that you don't have a project that is a "mix" of MVC and web forms.

You have this choice:

You can create a web forms project, or an MVC project, or an API project. That does not hint nor suggest nor imply that you can create a webforms + MVC project. Not even close!

Again, it suggests and hints NO such projects.

Let's click on that and see the next panel:

So, you get to name the project, choose the .NET Framework.

However, we still have not selected the project type.

Next page:

So you do not see any project that says MVC + web forms, you do not see ANY project that suggests a mix.

At this point in time, you can choose an empty project:

(a asp.net web forms project).

Or you can choose a Web Forms project - NOT MVC.

Or you can choose an MVC project - NOT web forms!

So, NO you DO NOT HAVE an MVC + web forms project template, and none exist.

You can create an MVC project, or you can create a WebForms project.

As I stated, if you want to create an MVC project and consume aspx pages, then you can do this, and how you can do this is outlined here:

https://stackoverflow.com/questions/25385532/how-to-add-aspx-pages-to-existing-mvc-4-project

However, as above shows, you WILL have created an MVC project for above to work.

Again, there is NOT a template choice that is some magic mix of MVC + web forms. Such projects and templates don't exist.

So, yes, you can consume aspx pages, but you're not going to have some automatic, or auto "magic" template to do this, and you have to adopt code, and it is a rather manual coding process that YOU the developer's efforts here will have to make such a setup work.

IMHO it tends to be a rather bad idea, since you lose lots of MVC support stuff, and you lose lots of WebForms support stuff - you, in fact, wind up with the worst of both systems, and will be hampered using MVC, and you'll be hampered using WebForms. As the above link shows, this can be done, but you WILL NOT and DO NOT have an out-of-the-box template system that is an MVC + WebForms combined project. You, the developer, will have to write some hack code and tricks to make this work.

英文:

A web site built around MVC is vastly different then the architecture of a asp.net webforms site.

So, while you CAN use aspx pages in a MVC site, it not a default, and you require a good deal of "tricks" to make this work.

> The views provided are either .aspx or ascx files

Those are NOT called views, and you can't make such a statement any more then trying to say a glass of water and a dog are the same thing.

If you site has ZERO cshtml pages now, you do NOT have a MVC site, but you have a asp.net webforms site.

A aspx web page is a web forms page. There are no views in such pages.

You will have a aspx page with markup, and then a corresponding code behind page.

So, if you have a page called Fighters.aspx, then you will have (in most cases) a corresponding code behind page called Fighers.aspx.cs (or Fighters.aspx.vb if this is a vb.net site).

So, you see this in the project tree:

Html.RenderPartial方法的协助,传递一个带有模型的ascx文件。

There are no cshtml pages in such a site, you can't add them, and they have nothing to do with a asp.net web forms site.

Different design, different architecture, and different operation.

> so I can't implement it with .cshtml files

There are no such types of files in a webforms project.

And keep in mind there are two types of webform projects.

There is a asp.net webforms site

And then there is what is called a asp.net webforms application

The big difference is the lack of the word "application".

You can in about 5 seconds of time know the difference, and a webforms site will not have the "designer" files like a web application site has.

For a web site, you go file->open web site.

For a web site application, you file->open Project.

I should also point out that while MVC sites can be either .net core (the newer framework), asp.net web sites, or asp.net web site "applications" can ONLY be built using the .net framework, and NOT with .net core.

So, if was never any cshtml files in that project, then you do NOT have a MVC site. You can perhaps get some cshtml pages to work, but I don't think you should mess around trying to do that in this case.

As I noted, you CAN load and make aspx pages work in a MVC site, but you from what you provided don't have a "mvc" site.

However, if your posted code is from that project?

Then it looks like you may well have a MVC site, and it is loading + consuming asp.net "aspx" pages. While this is possible, it is somewhat convoluted approach here.

I would right click while viewing that aspx web page, and choose view code - you should see the code behind page for that aspx page.

We will likely require more details here, but that posted page of yours does not look correct, and it looks like a asp.net webforms master page, with some extra junk tossed in that does not really look correct.

If some pages in question REALLY do have this:

 &lt;% Html.RenderPartial(&quot;~/Menu.ascx&quot;, Model); %&gt;

Then OK, this means you do have some views, and do have some cshtml pages. However, in most cases, the reverse is done. So, if there are in fact some cshtml files in that project, then I stand corrected.

(You may well have a cshml page and it loads + uses a aspx page, or perhaps a ascx control).

I mean, when you open this project, (the sln file), does the site build + run when you hit f5?

Edit: The choice to create a MVC or webforms

Let's try this again. I stated that you don't have a project that is a "mix" of MVC and web forms.

You have this choice:

Html.RenderPartial方法的协助,传递一个带有模型的ascx文件。

You can create a web forms project, or a MVC project, or a API project. That does not hint nor suggest nor imply that you can create a webforms + MVC project. Not even close!

Again, it suggests and hints NO such projects.

Let's click on that, and see the next panel:

Html.RenderPartial方法的协助,传递一个带有模型的ascx文件。

So, you get to name the project, choose the .net framework.

However we still not have selected the project type.

Next page:

Html.RenderPartial方法的协助,传递一个带有模型的ascx文件。

So you do not see any project that says MVC + web forms, you do not see ANY project that suggests a mix.

At this point in time, you can choose a empty project:

(a asp.net web forms project).

Or you can choose a Web Forms project - NOT MVC.

Or you can choose a MVC project - NOT web forms!

So, NO you DO NOT HAVE a MVC + web forms project template and none exist.

You can create a MVC project, or you can create a WebForms project.

As I stated, if you want to create a mvc project and consume aspx pages, then you can do this, and how you can do this is outlined here:

https://stackoverflow.com/questions/25385532/how-to-add-aspx-pages-to-existing-mvc-4-project

However, as above shows, you WILL have created a MVC project for above to work.

Again, there is NOT a template choice that is some magic mix of MVC + web forms. Such projects and templates don't exist.

So, yes, you can consume aspx pages, but you not going to have some automatic, or auto "magic" template to do this, and you have to adopt code and it is a rather manual coding process that YOU the developer efforts here will have to make such a setup work.

IMHO it tends to be a rather bad idea, since you lose lots of MVC support stuff, and you lose lots of WebForms support stuff - you in fact wind up with the worst of both systems, and will be hampered using MVC, and you be hampered using WebForms. As the above link shows, this can be done, but you WILL NOT and DO NOT have a out of the box template system that is a MVC + WebForms combined project. You the developer will have to write some hack code and tricks to make this work.

huangapple
  • 本文由 发表于 2023年6月15日 08:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定