Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let's assume, we have a view: "ViewProduct.cshtml". Now let's say we want to create simple AddProduct GET and POST actions. Is it possible to auto-generate AddProduct GET version by doing this:

Render exactly what is in ViewProduct.cshtml, but replace each @Model.$property$ of type string (like Model.Title, Model.Content, but not Model.Date f.e.) by @Html.TextBoxFor(m => m.$property)? How could I achieve it programmatically?

The concept is something like this:
public ProductController
{
public ActionResult ViewProduct() { ... }
public ActionResult CMSViewProduct()
{
// PSEUDOCODE I look for implementation of.
string html = GetHTMLCode("ViewProduct.cshtml");
html = html.Replace(@Model, ...)
return View(html); // here html would contain textboxes for each @Model.#property# where property is of string type.
}
}

What I have tried:

I have already built the whole ViewProduct.cshtml and I have controllers ready. I scrutinized Html.Render (RenderPartial, Partial) in particular, but it doesn't help, since I need something that would first take the whole code from ViewProduct.cshtml and then replace and add textboxs BEFORE sending html to user so that If I add new property inside Model and ViewProduct in the future, CMS will be immediately ready to go.
Posted
Updated 27-Jun-16 10:32am
v2

1 solution

Hi,

Yes you can simple use Ajax request and pass parameter value as you want. You can set value to JS function using your @Model object.

AjaxUrl = ControllerName + Fucntion Name


JavaScript
$.ajax({
            type: "GET",
            url: Ajaxurl,
            cache: false,
            data: { Id: Id },
            error: function() {
                alert("An error occurred.");
            },
            success: function(data) {
              alert("Success.");
           
            }

        });
 
Share this answer
 
Comments
EnGiNe.Pl 27-Jun-16 16:29pm    
I'm sorry for not being precise enough - this must be performed server-side. I updated the question now.
Tabish Mateen 27-Jun-16 20:45pm    
There are multiple options that can be used based on your scenario.

First Approach :
--------------------
Use conditional statement inside you view and based on it render partial view. Fast and easy to implement.

Example :

if(@Model.Type = "First")
{
@Html.Partial("_First")
}
else
{
@Html.Partial("_Second",@Model.SecondData)
}


Second Approach :
-------------------

Render partial view from code behind based on your criteria.

Example:

return PartialView("_Details",DetailsData);

Hope this will answer your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900