2

Im using mvc4 and im getting JSON from http://api.feedzilla.com/v1/categories.json

My model has the following code

public class catagorygroup
    {

        public List<CatagoryModel> catagoryModel { get; set; } 

    }

    public class CatagoryModel
    {

        public int category_id { get; set; }
        public string english_category_name { get; set; }
    }

my view is like this

        @for (int i = 0; i < Model.catagoryModel.Count; i++)
        {
            using (Html.BeginForm("News", "Catagory"))
            {

            <li> <input type="submit"   name="w8-red" class="w8-button red"  value= @Model.catagoryModel[i].english_category_name   /> </li>
        @Html.HiddenFor(model => model.catagoryModel[i].category_id);
        @Html.HiddenFor(model => model.catagoryModel[i].english_category_name); 

        <br/><br/>
            }
            }     

and my view is like this

enter image description here

If i click the First button Sports Button Im getting the "Name,Id " value like this

enter image description here

but if i click any button other than the first button im getting something like this [Null values] enter image description here

What went wrong in my code

3
  • u are making multiple forms in a single page. !! Commented Jul 30, 2013 at 11:14
  • Yes .and each buttons like "Sports,Arts,Blog,etc" are submit buttons having its own Form Commented Jul 30, 2013 at 11:17
  • @AshokDamani But it will post the entire Model right , I need to get only the "category_id,english_category_name" of the selected button , For example if i click "Sports= i will get some id and name " similarly if i click "Art= it should give only the art id and name " got it Commented Jul 30, 2013 at 11:21

2 Answers 2

1

That looks like it may be better suited as an anchor tag.

@Html.ActionLink(model.catagoryModel[i].english_category_name,
                 "News",
                 "Catagory",
                 new { @Model.catagoryModel[i].english_category_name,
                       @Model.catagoryModel[i].category_id },
                 new { @class = "w8-button red" });

See: LinkExtensions.ActionLink

Sample:

//HomeController.cs

public class HomeController : Controller
{
     public ActionResult Index()
     {
         return View();
     }

    public ActionResult DoWork(SomeDTO dto)
    {
        return View("Index");
    }
}

public class SomeDTO
{
    public int SomeId { get; set; }
    public string SomeData { get; set; }
}

//Index.cshtml

@Html.ActionLink("Home", "DoWork", new { SomeId = 1, SomeData = "World" })

//RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home",
                            action = "Index",
                            id = UrlParameter.Optional }
        );
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

NO im getting error /Category/News?english_category_name=Blogs&category_id=21 and HTTP 404 error how can i get those values in controller
Remove the HttpPost attribute on the News method.
No , Im getting the value in the URL like this Category/News?english_category_name=Blogs&category_id=21 but stil HTTP 404 error
I edited in a sample for you. However, you might have some routing issues if an action link is not working.
@Romoku. Thanks for your code . And can you kindly help me how to resolve routing problem
|
0

Check out this, it may works

@Html.ActionLink(model.catagoryModel[i].english_category_name,
             "News",
             new { @Model.catagoryModel[i].english_category_name,
                   @Model.catagoryModel[i].category_id },
             new { @class = "w8-button red" });

Comments

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.