0

I need to send model parameter from div to script. This is my not working .cshtml file

@model List<Dialog>
@foreach (Dialog dialog in Model)
{
    <div onclick="SelectDialog(@dialog)"></div>
}
<script>
    function SelectDialog(dialog) {
        //work with dialog
    }
</script>

How can I send current dialog from view to script?

Bashnia007
  • 135
  • 13

2 Answers2

0

When addressing a model in the first line you should:

Index.cshtml

 @model IEnumerable<YourProject.Models.Dialog>

or

 @model YourProject.Models.Dialog

col1, col2 and col3 are your class objects

Class.cs

namespace YourProject.Models
{
public class Dialog    {
    [Key]
    public int id { get; set; }
    public string col1 { get; set; }
    public string col2 { get; set; }
    public string col3 { get; set; }
    }
}

HomeController.cs

public class HomeController : Controller
{
    public ActionResult Index()
    {
        List<Dialog> all = new List<Dialog>();
        ...
        return View(all);
    }
}
er_jack
  • 114
  • 9
  • Yes, i know and have done it. I need to work with each dialog in script at my view when user click on the div – Bashnia007 Apr 15 '15 at 13:06
0

Instead of Dialog object, can you achieve the desired functionality by passing parameters to SelectDialog? For example: Instead of

function SelectDialog(Dialog d)
{
  if (d.id == 1)
    alert('first id');
  else
    alert('not first');
}
write:
function SelectDialog(int d)
{
  if (d == 1)
    alert('first id');
  else
    alert('not first');
}
TechTurtle
  • 2,667
  • 4
  • 22
  • 31