locked
How to show a PartialView with Model "With null model" RRS feed

  • Question

  • User283528319 posted

    Hi all,

    I have a PartialView which I want to show in a View like the below code and I want to show it empty at the load of the page (View).

    Then using Jquery I will call it again and fill it <g class="gr_ gr_312 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="312" data-gr-id="312">when ever</g> I need.

    Could you help me please without having <g class="gr_ gr_231 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="231" data-gr-id="231">error</g> that Model <g class="gr_ gr_336 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="336" data-gr-id="336">is not exist</g> or null or <g class="gr_ gr_383 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation multiReplace" id="383" data-gr-id="383">somet</g>hing.

    @model IQueryable<EvrakaCore.Models.SevkedilenLablar>
    
    <tbody>
        @foreach (var item in Model)
        {
            <tr id=@item.SevkedilenLablarId>
                <td>
                    @Html.DisplayFor(modelItem => item.Birimler.BiriminKisaAdi)
                </td>
                <td>
                    <button name="LabSilBtn" class="btn btn-outline-primary float-sm-right">Sil</button>
                </td>
            </tr>
        }
    </tbody>

    I have 

    Tuesday, March 26, 2019 10:00 AM

All replies

  • User475983607 posted

    Add a conditional statement but you'll need to execute the query first. 

    I recommend that you pass a model to PartialView rather than a query.

    Untested

    <tbody>
    @{var items = Model.ToList();}
    @(if items != null && items.Count() > 0)
    {
        foreach (var item in items)
        {
            <tr id=@item.SevkedilenLablarId>
                <td>
                    @Html.DisplayFor(modelItem => item.Birimler.BiriminKisaAdi)
                </td>
                <td>
                    <button name="LabSilBtn" class="btn btn-outline-primary float-sm-right">Sil</button>
                </td>
            </tr>
        }
    }
    </tbody>

    Tuesday, March 26, 2019 12:06 PM
  • User283528319 posted

    that is not the solution.

    I need to show "empty" version of the elements <g class="gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation replaceWithoutSep" id="6" data-gr-id="6">therefore</g> I kinda need

    create an empty Model and show its fields empty (if the model is empty).

    Tuesday, March 26, 2019 12:14 PM
  • User475983607 posted

    that is not the solution.

    I need to show "empty" version of the elements therefore I kinda need

    create an empty Model and show its fields empty (if the model is empty).

    Add and else to the condition and render HTML inputs as needed.

    <tbody>
    @{var items = Model.ToList();}
    @(if items != null && items.Count() > 0)
    {
        foreach (var item in items)
        {
            <tr id=@item.SevkedilenLablarId>
                <td>
                    @Html.DisplayFor(modelItem => item.Birimler.BiriminKisaAdi)
                </td>
                <td>
                    <button name="LabSilBtn" class="btn btn-outline-primary float-sm-right">Sil</button>
                </td>
            </tr>
        }
    }
    else {
    	        <tr>
                <td>
                    <input type="text" name="BiriminKisaAdi" />
                </td>
                <td>
                    <button name="LabSilBtn" class="btn btn-outline-primary float-sm-right">Sil</button>
                </td>
            </tr>
    }
    </tbody>

    Tuesday, March 26, 2019 12:22 PM
  • User283528319 posted

    mgebhard

    dd and else to the condition and render HTML inputs as needed.

    it is not the solution

    that means I have to have 2 code parts that I need to constantly change and make  similar

    that doubles my job

    that is why I am here to ask the other solution

    Tuesday, March 26, 2019 1:34 PM
  • User283528319 posted

    The thing is there is no RenderAction in <g class="gr_ gr_34 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="34" data-gr-id="34">.net</g> core (in contrary <g class="gr_ gr_41 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="41" data-gr-id="41">of</g> <g class="gr_ gr_52 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="52" data-gr-id="52">netframework</g>)

    if there was RenderAction I could <g class="gr_ gr_85 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="85" data-gr-id="85">easly</g> create a Model in Controller. But I am using RenderPartial now and got <g class="gr_ gr_190 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="190" data-gr-id="190">partial</g> view directly without <g class="gr_ gr_180 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="180" data-gr-id="180">manuplating</g> by <g class="gr_ gr_191 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="191" data-gr-id="191">controller</g>.

    Tuesday, March 26, 2019 2:03 PM
  • User475983607 posted

    fatihbarut

    it is not the solution

    that means I have to have 2 code parts that I need to constantly change and make  similar

    that doubles my job

    We can only see the code that you provide.  If you are looking for an alternative solution then you need to tell us.

    fatihbarut

    that is why I am here to ask the other solution

    Use standard MVC and coding patterns.

    Most likely the partial view is not needed.  I would remove the partial and just use a regular view.   You can always refactor the code to include the partial at a later time.

    Execute the query in the controller or business layer not in a Partial View.  Build a ViewModel that tells the View how to render.  For example, return a "Strongly Typed Create View" (from the action)  if the query returns an empty result set.  

    You'll need to clarify your requirements if you need further design assistance.

    Tuesday, March 26, 2019 2:07 PM