Asked  7 Months ago    Answers:  5   Viewed   53 times

I have this section defined in my _Layout.cshtml

@RenderSection("Scripts", false)

I can easily use it from a view:

@section Scripts { 
    @*Stuff comes here*@

What I'm struggling with is how to get some content injected inside this section from a partial view.

Let's assume this is my view page:

@section Scripts { 

        //code comes here

    poo bar poo


I need to inject some content inside the Scripts section from _myPartial partial view.

How can I do this?



Sections don't work in partial views and that's by design. You may use some custom helpers to achieve similar behavior, but honestly it's the view's responsibility to include the necessary scripts, not the partial's responsibility. I would recommend using the @scripts section of the main view to do that and not have the partials worry about scripts.

Tuesday, June 1, 2021
answered 7 Months ago

You need to add the .cshtml extension to the view name:

return PartialView("~/Views/Shared/MyCustomFolder/_MyPartialView.cshtml",PartialViewModel);
Tuesday, August 3, 2021
answered 4 Months ago

In order to do this for multiple items do something like:

foreach (var item in Model)
    @Html.RadioButtonFor(m => m.item, "Yes") @:Yes
    @Html.RadioButtonFor(m => m.item, "No") @:No
Tuesday, August 3, 2021
answered 4 Months ago

Your button click is doing this:

location.href='@Url.Action("CheckAnswer", "RazerQuestion", new { id = 'A' })    '"

which navigates the browser to the partial page.

I suspect what you want to happen is for the partial page content to be replaced with the updated content without refreshing the browser, or navigating away.

You can do with with JQuery.

First, give your an id:

<div id="content" class="transbox" style="height:inherit;"> 

Second, do something similar to the following in response to the button click (error checking removed):

function doWork(){
    $.get('@Url.Action("CheckAnswer", "RazerQuestion", new { id = 'A' }'), function (data) {

Finally, change the button to do this:

<input id="Button1" type="button" value="button" onclick="doWork()" />

This will now:

  • On click of the button...
  • Fire the doWork() function, which will...
  • Make the request to the CheckAnswer action, and then...
  • Take the content of the request, and replace the <div /> content with the result
Friday, September 3, 2021
answered 3 Months ago

Ok, this will get it working for you.

Create.cshtml View (With the form & submit moved outside of the partial)

@using(Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
   @Html.Partial("_UploadFiles", Model)
   <input type="submit" value="submit" />

_UploadFiles.cshtml view

@model ModelToCreate

@Html.TextBoxFor(m => m.Files.Files, new { type = "file", name = "Files" })

Models (Changed to a List and also note the initializer in the FileUploadModel constructor).

public class ModelToCreate
    //Some properties
    public FileUploadModel Files { get; set; }

public class FileUploadModel
   public FileUploadModel()
        Files = new List<HttpPostedFileBase>();

   public List<HttpPostedFileBase> Files { get; set; }

Controller actions:

public ActionResult Create()
    var model = new ModelToCreate();

    return View(model);


public ActionResult Create(ModelToCreate model)
   var file = model.Files.Files[0];
   return View(model);

enter image description here

Tuesday, November 16, 2021
Juraj Blaho
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :