Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm new to MVC.

on MSDN i've studied that there should be folder in view with the same name of controller. For every Action Method in the controller we have to create a View in the same folder.

I'm creating a test application in which:

I have a homeController with an Index ActionMethod. Corresponding to it i have a View in View/home/Index, which simply show the listing of the employees.

I know i can add a [HTTP POST] Index ActionMethod in the homeController.

But i want to add the Delete and Search functionality on the view. So that a user can search the employees with there name and can delete an employee on the same page.

I don't know how can i move ahead for this functionality.

Still i'm using this code.

homeController

public ActionResult Index()
    {
        ViewBag.text = "Records Listing";
        var q = from p in objEmp.tbemployees select p;
        return View(q);
    }

Index.cshtml

        @model IEnumerable<MvcApplication6.Models.tbemployee>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>@ViewBag.text</h1>
<table style="font-size:15px;">
    <tr>
        <th>
            Name
        </th>
        <th>
            Address
        </th>
        <th>
            Sallary
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr >
            <td style="padding:7px;">
                @Html.DisplayFor(mm => item.ename)
            </td>
            <td style="padding:7px;">
              @Html.DisplayFor(mm => item.eadd)
            </td>
            <td style="padding:7px;">
              @Html.DisplayFor(mm => item.esal)
            </td>
              <td style="padding:7px; color:Blue; text-decoration:underline;">
            @Html.ActionLink("Edit", "Edit", new { id = item.empno })
            </td>

        </tr>
    }
</table>

Thanks.

share|improve this question

2 Answers

For the Delete you could add a column in the table that will invoke a controller action and pass it the current record id:

<tr>
    <td style="padding:7px;">
        @Html.DisplayFor(mm => item.ename)
    </td>
    <td style="padding:7px;">
        @Html.DisplayFor(mm => item.eadd)
    </td>
    <td style="padding:7px;">
        @Html.DisplayFor(mm => item.esal)
    </td>
    <td style="padding:7px; color:Blue; text-decoration:underline;">
        @Html.ActionLink("Delete", "Delete", new { id = item.empno })
        @Html.ActionLink("Edit", "Edit", new { id = item.empno })
    </td>
</tr>

and your Delete action:

public ActionResult Delete(int id)
{
    ... use the passed id to delete the record from the database
    return RedirectToAction("Index");
}

for the Edit functionality you could have a controller action that will fetch the record and render a view that will allow for editing:

public ActionResult Edit(int id)
{
    var employee = objEmp.tbemployees.FirstOrDefault(x => x.Id == id);
    if (employee == null)
    {
        // no employee with the specified id was found
        return new HttpNotFound();
    }
    return View(employee);
}

and then you could have a corresponding ~/Views/Home/Edit.cshtml view:

@model Employee
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.ename)
        @Html.EditorFor(x => x.ename)
    </div>
    <div>
        @Html.LabelFor(x => x.eadd)
        @Html.EditorFor(x => x.eadd)
    </div>

    ...

    <button type="submit">Save</button>
}

and of course a corresponding action to update the record when this form is submitted:

[HttpPost]
public ActionResult Edit(Employee employee)
{
    ... update the employee record
    return RedirectToAction("Index");
}
share|improve this answer
I have already created the edit functionality and view for it. As your code shows, i don't wanna add a new view for the delete. I wanna perform it on the same Index view. – Code Rider Apr 22 at 6:32
Thanks. I tried the code. Its working. One thing i wanna ask that, According to your code i have added a Delete ActionMethond in controller. But i haven't created an view for it. Isn't it required? – Code Rider Apr 22 at 6:37
No, it is not required, because the Delete action redirects to the Index action. It doesn't return a View, so you don't need a view. – Darin Dimitrov Apr 22 at 6:46
and what about the search functionality? I have to search the records and have to return the records from controller to view on search button. How can i achieve this? – Code Rider Apr 22 at 6:54
You could have a controller action that will render a view containing an HTML form with the corresponding fields you want to filter with. This HTML form could submit to a controller action that will filter the records and render the Index view with those filtered records: return View("Index", filteredEmployees);. – Darin Dimitrov Apr 22 at 7:45
show 2 more comments

You can add and implement a Delete action method in your controller. Then in your view, call @Html.ActionLink("Delete", "Delete", new { id = item.empno }). This will return a hyperlink which links to your Delete method in the controller.

share|improve this answer
and what about the search functionality? – Code Rider Apr 22 at 6:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.