ASP.NET MVC RenderPartial Error Code CS0029 - c#

Not sure what went wrong, but the error is "CS0029: Cannot implicitly convert type 'void' to 'object'" at the Html.Partial.The other post said it should be enclosed with curved brackets, but its still not working (with and without). I have tried with Html.RenderPartial and Html.Partial, both don't work.
<div class="row" id="dtRecipe" style="margin-top:15px">
#*#{
Html.RenderPartial("_ListRecipe", new List<Eatery.Model.Models.Recipe>(), ViewData);
}*#
#Html.Partial("_ListRecipe", new List<Eatery.Model.Models.Recipe>(), ViewData);
</div>
The code for _ListRecipe is
public async Task<ActionResult> _ListRecipe()
{
int DishID = Convert.ToInt32(ViewData["DishID"]);
List<Recipe> recipes = (await recipeService.Get(DishID)).ToList();
return PartialView(recipes);
}
The recipeService is okay as it is used in another file.

Remove the ; at the end, it should be (without razor code block syntax):
#Html.Partial("_ListRecipe", new List<Eatery.Model.Models.Recipe>(), ViewData)
If you want to properly use Html.RenderPartial, this is the right type of syntax (See code below), which means you need to create a razor syntax code block and there is a ; at the end:
#{
Html.RenderPartial("_ListRecipe", new List<Eatery.Model.Models.Recipe>(), ViewData);
}

Related

Adding await to RenderPartialView produces an error

This is an ASP.net core web application view. This works:
#if (r.Identifier.HasAcronym)
{
#Html.RenderPartialAsync("Resources", new Tuple<IEnumerable<FileSystemObject>, string>(r.ResourcesByAcronym, r.Identifier.Acronym));
}
this produces the error: Cannot implicitly convert type 'void' to 'object'
#if (r.Identifier.HasAcronym)
{
#await Html.RenderPartialAsync("Resources", new Tuple<IEnumerable<FileSystemObject>, string>(r.ResourcesByAcronym, r.Identifier.Acronym));
}
You can write like this:
<td>
#{
await Html.RenderPartialAsync("Resources", new Tuple......);
}
</td>
Html.RenderPartialAsync returns a Task, so the result type is void. The # without braces asks Razor to render the result, which is not possible: There is no way to convert void to anything renderable. This would only be the right way to go if Html.RenderPartialAsync returned a string, which needed to be rendered manually. The braces introduce a new code block, which allows standard C# syntax.

System.Web.HttpParseException: 'Unexpected "{" after "#" character

I'm getting this exception while trying to render PartialView. When I change #{Html.RenderPartial("_ChildReplies", parRep.ChildReplies);}
to
#Html.Partial("_ChildReplies", parRep.ChildReplies) still getting same exception.
#model List<Reply>
#using YourPlace.Models
<ul>
#foreach (var parRep in Model)
{
<li>
Author: #parRep.AuthorName
Comment: #parRep.AuthorName
<div>
#{Html.RenderPartial("_ChildReplies", parRep.ChildReplies);}
</div>
</li>
}
</ul>
The code above looks correct... if you are getting this error, it is probably because something is going wrong inside _ChildReplies partial view which you have not shown in your question...
Good explanation here:
RenderPartial() is a void method that writes to the response stream. A void method, in C#, needs a ; and hence must be enclosed by
{ }.
Partial() is a method that returns an MvcHtmlString. In Razor, You can call a property or a method that returns such a string with just a
# prefix to distinguish it from plain HTML you have on the page.
So you need to either use this:
#{ Html.RenderPartial("_ChildReplies", parRep.ChildReplies); }
Or this:
#Html.Partial("_ChildReplies", parRep.ChildReplies);

Load Partial View inside another Partial View

I have a PartialView (_Letra) that receives information from a Controller named Music ... this way
public ActionResult CarregarLetra(string id, string artista, string musica)
{
return PartialView("_Letra", ArtMus(artista, musica));
}
public ResultLetra ArtMus(string artista, string musica)
{
//Conteúdo do metodo[..]
var queryResult = client.Execute<ResultLetra>(request).Data;
return queryResult;
}
Until then, no problem. What happens is that now I need to pass other information to this same PartialView (_Letra). This information is in PartialView (_Cifra).
So I added the following lines in my Music controller
public ActionResult CarregarCifra(string id, string artista, string musica)
{
return PartialView("_Cifra", GetCifra(artista, musica));
}
public ResultChords GetCifra(string artista, string musica)
{
var cfrTest= new Cifra();
var cifra = new ResultChords();
cifra.chords = cfrTest.GetInfs(artista, musica);
return cifra;
}
Everything working so far, PartialView _Cifra receives the information
I searched and found that I could use in PartialView _Letra the Html.Partial to load my PartialView _Cifra, I did this way then
I added
<div class="item">
<div class="text-carousel carousel-content">
<div>
#Html.Partial("_Cifra", new letero.mus.Infra.Models.ResultChords());
</div>
</div>
</div>
Now it starts to complicate why, the return of this is null, I believe it is due to a new instance of ResultChords that I make in Html.Partial
I have already tried using a ViewBag also to transpose the information between Partials, but probably not correctly, due to the return being null as well.
I've already done a lot of research and I'm not getting the information I need for PartialView _Letra.
There is a better way not to use Html.Partial, or to use it properly, as I am not aware.
In _Letra use
#Html.Action("CarregarCifra", "Music", new { id=Model.Id, artista=Model.Artista, musica=Model.Musica });
if the variables are available on the model then you can pass them in; otherwise, make use of the Viewbag and set them in CarregarLetra
Are you always passing a new object to the second partial? You could just create it at the top of the new _Cifra partial.
_Cifra.cshtml
#{
var resultChords = new letero.mus.Infra.Models.ResultChords();
}

MVC - Variable in .cshtml file which updates the html when the controller calls it

this is probably a pretty basic thing for you guys, but I still can't figure it out.
Let's say I have a View:
myView.cshtml
<div>
<p>Some content here</p>
#MyHTMLVariable //Calls updateHTML -> MyHTMLVariable = s
<p>Some more content over here</p>
</div>
myController.cs
public string updateHTML()
{
s = "I'm a string"; //Changes dynamically, handled by different stuff outside
//Any string to html-conversion needen?
return s;
}
How can I "update" the variable in the view / how do I have to initialize it?
Cheers,
DDerTyp
Use Razor's #functions code block to declare your functions within your view this way...
#functions {
public string updateHtml()
{
string s = "I'm a string";/*or whatever dynamic stuff you wanna do*/
return s;
}
}
#{
string myHtmlVariable = updateHtml();
}
Then use #myHtmlVariable within your code.
You can use the ViewBag for this. In your template use #ViewBag["MyHTMLVaribale"] and in your controller method use ViewBag["MyHTMLVariable"] = "I'm a string";

Implementing Forms in ASP.net MVC

I have a simple form on a view page, implemented as a user control, that looks something like this:
<%=Html.BeginForm("List", "Building", FormMethod.Post) %>
//several fields go here
<%Html.EndForm(); %>
There are two problems I would like resolved, the first is that I would like the controller method that receives this to take a type parameter of the user control. The goal is to avoid putting all of the fields of the form into the parameter list for the method. The controller method currently looks like this:
[AcceptVerbs("Post")]
public ActionResult List(string capacityAmount)
{
ProfilerDataDataContext context = new ProfilerDataDataContext();
IEnumerable<Building> result = context.Buildings.OrderBy(p => p.SchoolName);
ViewData["Boroughs"] = new SelectList(Boroughs.BoroughsDropDown());
return View(result);
}
The rest of the fields in the form will be used to conduct a search against the buildings type.
The form posts fine, I can search on the capacity the way you would expect, but I can smell ugliness ahead as I add parameters to the search.
Second, smaller problem is that when the page renders the BeginForm tag renders the string "System.Web.Mvc.Form" to the page. How do I make that go away?
1) Use FormCollection as the argument:
public ActionResult List(FormCollection searchQuery)
Now you can iterate the FormCollection and get key/value search terms from your search form.
2) Remove the "=" from BeginForm:
<% Html.BeginForm("List", "Building", FormMethod.Post) %>
That said, you should really be using, um... using:
<% using (Html.BeginForm("List", "Building", FormMethod.Post)) { %>
<% } %>
If I'm understanding your question properly, you use the html helper and create inputs named:
<%=Html.TextBox("building.FieldNumber1")%>
<%=Html.TextBox("building.FieldNumber2")%>
You should be able to access the data using:
public ActionResult List(Building building)
{
...
var1 = building.FieldNumber1;
var2 = building.FieldNumber2;
...
}
and if your action is to do two different things depending on if form is submitted:
public ActionResult List()
{
//some code here
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult List(Building building)
{
...
var1 = building.FieldNumber1;
var2 = building.FieldNumber2;
...
}
if anybody is skeptical about the whole 'using' pattern with Html.BeginForm - realize that the IDE is smart enough to match the opening '{' with the ending '}' which makes it very easy to see where your form begins and ends.
Also <% Html.EndForm(); %> requires a semicolon which I'm not sure i like :)

Categories

Resources