Click here to Skip to main content
15,886,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
i have created a form with two buttons like submit and cancel.
i want to perform action for both method. but it doesn't work..


Here is my controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication8.Models;

namespace WebApplication8.Controllers
{
    public class personController : Controller
    {
        //
        // GET: /person/
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult disp(studentmodel model, string command)
        {
            try
            {
                if (command == "Create")
                {
                    return View("shows", model);
                }
                else if (command == "cancel")
                {
                    return RedirectToAction("Index", "person");
                }
            }
            catch(Exception e)
            {
                return View("Index");
            }
        }
  
	}
}



Here is my index html file:
@model WebApplication8.Models.studentmodel


@using (Html.BeginForm("disp","person")) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>studentmodel</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.name)
                @Html.ValidationMessageFor(model => model.name)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" name="command" class="btn btn-default" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="cancel" name="command" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}



[edit] Code block added - OriginalGriff[/edit]
Posted
Updated 19-Feb-14 20:00pm
v2
Comments
Maarten Kools 20-Feb-14 2:05am    
It doesn't work, as it doesn't compile? Your disp method does not return a value in all code paths. You have your if and else if, but nothing if command is neither "Create" or "cancel".
prabu19 20-Feb-14 2:20am    
Thanks buddy. i will try

1 solution

Look at your code:
public ActionResult disp(studentmodel model, string command)
{
    try
    {
        if (command == "Create")
        {
            return View("shows", model);
        }
        else if (command == "cancel")
        {
            return RedirectToAction("Index", "person");
        }
    }
    catch(Exception e)
    {
        return View("Index");
    }
}

What value is returned if command is not "Create" or "cancel"?

Your code doesn't specify, so VS rightly says that there is a way through the method that does not return any value - and you can't do that!
 
Share this answer
 

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