0

Say I want to create a form that allows you to add elements dynamically through javascript... for example, a project with many tasks...

$("#add_task").click(function () {
            $('#tasks').append('@Html.Raw(Html.Partial("_task_fields").ToHtmlString())');
        });

I seem to be having problems with this approach cause the javascript isnt encoded... and using HttpUtility.JavaScriptStringEncode adds extra quotation marks to the elements that have attributes like class"someClass" for instance.

my task_fields partial is something like this

<tr>
     <td>
         Task
     </td>
     <td>
        <input type="text" name="task[name]" />
     </td>
</tr>

I just want the add_task link to work properly.

Please help.

1

2 Answers 2

1

Ok. You will do it but little different way. Write action to controller instead of partial.

public class HomeController : Controller
{
        public ActionResult MyPartial(/*some parameters*/)
        {
            object someData = foo(/*some parameters*/);
            return PartialView("_MyPartial", someData);
        }
}

**And call action with Ajax.**

$("#add_task").click(function () {
$('#taskItem').load(@Url.Content("~/home/MyPartial"));
....
})

Of course you can add to "task" after loaded "taskItem".

Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure this wont have the same problem?.
yes. I actualy use it on my some pages. but please do not forget, MyPartial call _MyPartial. _MyPartial should be in shared or Home path.
0

Have a look at jquery the jquery template plugin. It's a different but IMO much cleaner approach for this scenario.

Apart from using a jquery template only approach, you could also render the partial into a jquery template block and combine both technologies this way if you want.

3 Comments

hi Adrian, we decided to use jquery template plugin in our project, and we prepared some of them. It is not discussion place but I am wondering about performance and scalability. What do you think it is right way?
@Nuri: I'm using it in my current project (logmytime.de) throughout the website and am very happy with the results. I am only showing up to 50 or so jquery template instances per web page, but for scnenarios like that scalability has never been an issue.
thanks for you information, and I like your site! look like really nice!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.