<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=415099&amp;view=findpost&amp;p=3802536</guid>
        <pubDate>Tue, 02 Jul 2019 12:45:13 +0000</pubDate>
        <title>Razor Page Partial</title>
        <link>https://forum.sources.ru/index.php?showtopic=415099&amp;view=findpost&amp;p=3802536</link>
        <description><![CDATA[Wound: Кажется я понял как это делать.<br>
Нашел решение вот тут: <a class='tag-url' href='http://qaru.site/questions/7163065/how-to-return-a-partialview-from-core-2-razorpage-viewmodel-handler/14091432#14091432%2349183026' target='_blank'>http://qaru.site/questions/7163065/how-to-...1432%2349183026</a><br>
Для моего случая метод-обработчик, который возвращает частичное представление будет выглядеть вот так:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;public IActionResult OnGetCatIdInfo(string catId)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var Cat = new CatId()</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Id = catId,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Family = &quot;CatFamily Selected&quot;,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Model = &quot;CatModel Selected&quot;,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Location = &quot;CatLocation Selected&quot;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var myViewData = new Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary()) { { &quot;_CatIdInfoPartial&quot;, Cat } };</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myViewData.Model = Cat;</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PartialViewResult result = new PartialViewResult()</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ViewName = &quot;_CatIdInfoPartial&quot;,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ViewData = myViewData,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return result;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script><br>
Результат сразу можно передать в какой нибудь div как html.<br>
Что то типа:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;$.ajax({</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;url: &#39;/Index?handler=CatIdInfo&amp;catId=&#39; + catId,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;type: &#39;GET&#39;,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;contentType: &quot;application/json&quot;,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;success: function (cat) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.getElementById(&#39;catInfoContent&#39;).innerHTML = cat;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;},</div><div class="code_line">...</div></ol></div></div></div></div>]]></description>
        <author>Wound</author>
        <category>.NET: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=415099&amp;view=findpost&amp;p=3802531</guid>
        <pubDate>Tue, 02 Jul 2019 08:39:55 +0000</pubDate>
        <title>Razor Page Partial</title>
        <link>https://forum.sources.ru/index.php?showtopic=415099&amp;view=findpost&amp;p=3802531</link>
        <description><![CDATA[Wound: Всем привет.<br>
<br>
Может кто то пояснить как работают частичные представления на уровне Razor классов наследуемых от PageModel ?<br>
Вот допустим есть у меня частичное представление _CatIdInfoPartial:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">@model CatWebApi.Models.CatId</div><div class="code_line">&nbsp;</div><div class="code_line">@using Microsoft.AspNetCore.Mvc.Localization</div><div class="code_line">@inject IViewLocalizer Localizer</div><div class="code_line">@{</div><div class="code_line">&nbsp;&nbsp; &nbsp;Layout = null;</div><div class="code_line">}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;</div><div class="code_line">&#60;div id=&quot;@Model.Id&quot; class=&quot;tabcontent bg-light box-shadow container border&quot;&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp;&#60;div class=&quot;container box-shadow&quot; style=&quot;max-height:92%;min-height:30%; overflow:auto&quot;&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&#60;table class=&quot;table table-striped&quot; style=&quot;border-collapse:separate&quot;&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;thead class=&quot;thead-dark&quot;&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;tr style=&quot;color:antiquewhite&quot;&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;th&#62;Cat Id&#60;/th&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;th&#62;Cat Family&#60;/th&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;th&#62;Cat Model&#60;/th&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;th&#62;Cat Location&#60;/th&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;/tr&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;/thead&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;tbody&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@for (int idx = 0; idx &#60; 2; ++idx)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;tr&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;td&#62;@Model.Id&#60;/td&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;td&#62;@Model.Family&#60;/td&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;td&#62;@Model.Model&#60;/td&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;td&#62;@Model.Location&#60;/td&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;/tr&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;/tbody&#62;</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&#60;/table&#62;</div><div class="code_line">&nbsp;&nbsp; &nbsp;&#60;/div&#62;</div><div class="code_line">&#60;/div&#62;</div></ol></div></div></div></div><br>
<br>
Дальше есть у меня класс обработчик, вызывается он через ajax запрос:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;&nbsp; &nbsp;[ValidateAntiForgeryToken]</div><div class="code_line">&nbsp;&nbsp; &nbsp;public class IndexModel : PageModel</div><div class="code_line">&nbsp;&nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;//! Вот этот обработчик вызывается когда я нажимаю на кнопку определенную на веб форме.</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;public PartialViewResult OnPostSelectCat(string catId)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CatId cat = null;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;foreach (var item in Cats)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (item.Id == catId)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cat = item;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Partial(&quot;_CatIdInfoPartial&quot;, cat); //! Вот примерно в этом месте летит исключение</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; }</div></ol></div></div></div></div><br>
И почему то при вызове этого обработчика летит исключение типа:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;An unhandled exception has occurred while executing the request.</div><div class="code_line">System.InvalidOperationException: The model item passed into the ViewDataDictionary is of type &#39;CatWebApi.Models.CatId&#39;, but this ViewDataDictionary instance requires a model item of type &#39;CatWebApi.Pages.IndexModel&#39;.</div><div class="code_line">&nbsp;&nbsp; at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(Object value)</div><div class="code_line">&nbsp;&nbsp; at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.SetModel(Object value)</div><div class="code_line">&nbsp;&nbsp; at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.set_Model(Object value)</div><div class="code_line">&nbsp;&nbsp; at Microsoft.AspNetCore.Mvc.RazorPages.PageModel.Partial(String viewName, Object model)</div></ol></div></div></div></div><br>
Ничего не понимаю. Если вызывать в самом html файле что то типа @{await Html.RenderPartialAsync(&quot;_CatIdInfoPartial&quot;, cat);} то все работает, а если через метод класса, то летит вот такое исключение. Притом что если передать ему то, что он просит в качестве модели, то будет лететь ровно такая же ошибка, но будет он просить уже то, что ему реально нужно передать. <br>
<br>
Может быть есть кто в курсе как с этим работать?]]></description>
        <author>Wound</author>
        <category>.NET: Общие вопросы</category>
      </item>
	
      </channel>
      </rss>
	